Skip to content

Commit

Permalink
feat: v4.1.0-beta.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Ketan Choyal committed May 17, 2023
1 parent fabf838 commit 726ee95
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 351 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Publish to Pub.dev

on:
push:
branches: [beta]
branches: [beta, master]

jobs:
publishing:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# [4.1.0-beta.1] - 17 May 2023
- Updated Readme with examples on how to use new added dart 3.0 features
- Introduced MapBoxSearch.init() method to initialize the API Key for the package (This is not a breaking change since the API Key can be passed to every class that uses the package)
- Breaking Change:
- Instead of throwing exceptions, the package now returns `ApiResponse` Record which can be either `Success` or `Failure` and can be handled using `fold` method.
- Location class is now converted to a record.
- List of coordinates are now converted to a record `(lat: double, long: doube)` whereever possible.

# [4.0.0-beta.2] - 15 May 2023
- Fixed Parsing issue for Search Results
- Added maki_icons to enum_generating script to keepup with new icons
- This will be last version to support dart < 3.0

# [4.0.0-beta.1] - 25 April 2023
## Breaking Changes
Expand Down
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

- PlaceSearch and ReverseGeoCoding classes are now merged into one class called GeoCoding with two methods `getPlaces` and `getAddress` since both of them are using the same API.
- MapBox's new [SearchBox API](https://docs.mapbox.com/api/search/search-box/) is added to the package.
- Instead of throwing exceptions, the package now returns `ApiResponse` Record which can be either `Success` or `Failure` and can be handled using `fold` method.
- Location class is now converted to a record.
- List of coordinates are now converted to a record `(lat: double, long: doube)` whereever possible.

This package provides easy api calls to MapBox Search API.

Expand All @@ -29,18 +32,30 @@ Then, add the following to your `pubspec.yaml` file:
dependencies:
mapbox_search: any

## Setup
Now you can setup the API key for whole app by calling `MapBoxSearch.init('API KEY')` in your `main()` function, this way you don't have to pass the API key to every class that uses the package.

### Usage of `ApiResponse`
```dart
final ApiResponse<List<MapBoxPlace>> addresses = await getAddress();
addresses.fold(
(success) => // Do something with success data,
(failure) => // Do something with failure data,
);
```

# Examples

### SearchBox API
```dart
SearchBoxAPI search = SearchBoxAPI(
apiKey: MAPBOX_KEY,
apiKey: 'API Key', // dont pass if you have set it in MapBoxSearch.init('API KEY')
limit: 6,
);
```
##### Get Suggestions
```dart
SuggestionResponse searchPlace = await search.getSuggestions(
ApiResponse<SuggestionResponse> searchPlace = await search.getSuggestions(
"central",
);
```
Expand All @@ -53,34 +68,36 @@ SearchBoxAPI search = SearchBoxAPI(

##### Get Place Details
```dart
RetrieveResonse searchPlace = await search.getPlace(mapboxId);
ApiResponse<RetrieveResonse> searchPlace = await search.getPlace(mapboxId);
```



### Reverse GeoCoding
```dart
var reverseGeoCoding = GeoCoding(
apiKey: 'API Key',
apiKey: 'API Key', // dont pass if you have set it in MapBoxSearch.init('API KEY')
limit: 5,
);
Future<List<MapBoxPlace>> geAddress() =>
Future<ApiResponse<List<MapBoxPlace>>> getAddress() =>
reverseGeoCoding.getAddress(
Location(lat: 72.0, lng: 76.00),
);
```



### Forward GeoCoding Seach
```dart
var geocoding = GeoCoding(
apiKey: 'API Key',
apiKey: 'API Key', // dont pass if you have set it in MapBoxSearch.init('API KEY')
country: "BR",
limit: 5,
types: [PlaceType.address, PlaceType.place],
);
Future<List<MapBoxPlace>> getPlaces() =>
Future<ApiResponse<List<MapBoxPlace>>> getPlaces() =>
geocoding.getPlaces(
"central park",
proximity: Location(
Expand All @@ -93,8 +110,8 @@ Future<List<MapBoxPlace>> getPlaces() =>
### Static Image
```dart
MapBoxStaticImage staticImage = MapBoxStaticImage(
apiKey:
"API Key");
apiKey: 'API Key', // dont pass if you have set it in MapBoxSearch.init('API KEY')
);
```

### Image With Polyline
Expand Down
17 changes: 11 additions & 6 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ final MAPBOX_KEY = '';

Future<void> main() async {
final apiKey = MAPBOX_KEY; //Set up a test api key before running
MapBoxSearch.init(apiKey);

await geoCoding(apiKey).catchError(print);
await placesSearch(apiKey).catchError(print);
Expand All @@ -15,17 +16,21 @@ Future<void> main() async {
///Reverse GeoCoding sample call
Future geoCoding(String apiKey) async {
var geoCodingService = GeoCoding(
apiKey: apiKey,
country: "BR",
limit: 5,
);

var addresses = await geoCodingService.getAddress(Location(
var addresses = await geoCodingService.getAddress((
lat: -19.984846,
lng: -43.946852,
long: -43.946852,
));

print(addresses);
addresses.fold(
(success) => print(success),
(failure) => print(failure),
);

print(addresses.success);
}

///Places search sample call
Expand All @@ -38,9 +43,9 @@ Future placesSearch(String apiKey) async {

var places = await placesService.getPlaces(
"patio",
proximity: Location(
proximity: Proximity.LatLong(
lat: -19.984634,
lng: -43.9502958,
long: -43.9502958,
),
);

Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
path: ".."
relative: true
source: path
version: "4.0.0-beta.2"
version: "4.1.0-beta.1"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -105,4 +105,4 @@ packages:
source: hosted
version: "3.0.7"
sdks:
dart: ">=2.19.0 <4.0.0"
dart: ">=3.0.0 <4.0.0"
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
homepage: https://github.com/ketanchoyal/mapbox_search

environment:
sdk: ">=2.17.0 <3.0.0"
sdk: ">=3.0.0"

dependencies:
mapbox_search:
Expand Down
10 changes: 10 additions & 0 deletions lib/mapbox_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ part 'models/location_context.dart';
part 'models/geometry.dart';
part 'models/suggestion_response.dart';
part 'models/retrieve_response.dart';
part 'models/failure_response.dart';

part 'generated_enums/poi_category.dart';
part 'generated_enums/maki_icons.dart';

Expand All @@ -20,3 +22,11 @@ part 'src/search_box_api.dart';
part 'src/geocoding_api.dart';
// part "src/maki_icons.dart";
part 'src/place_types.dart';

final class MapBoxSearch {
static String? _apiKey;

MapBoxSearch.init(String apiKey) {
_apiKey = apiKey;
}
}
26 changes: 23 additions & 3 deletions lib/models/bbox.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
part of mapbox_search;

class BBox {
Location min;
Location max;
final Location min;
final Location max;

BBox({
required this.min,
required this.max,
});

String get asString => '${min.lng},${min.lat},${max.lng},${max.lat}';
String get asString => min.asString + ',' + max.asString;

List<double> get asList => [
...min.asList,
...max.asList,
];

factory BBox.fromJson(Map<String, dynamic> json) => BBox(
min: (
long: json["bbox"][0],
lat: json["bbox"][1],
),
max: (
long: json["bbox"][2],
lat: json["bbox"][3],
),
);
factory BBox.fromList(List<dynamic> list) => BBox(
min: list.asLocation,
max: list.sublist(2).asLocation,
);
}
26 changes: 26 additions & 0 deletions lib/models/failure_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
part of mapbox_search;

class FailureResponse {
final String? message;
final String? error;
final Map<String, dynamic> response;

FailureResponse({
required this.message,
required this.error,
required this.response,
});

factory FailureResponse.fromJson(Map<String, dynamic> json) =>
FailureResponse(
message: json["message"],
error: json["error"],
response: json,
);

Map<String, dynamic> toJson() => {
'message': message,
'error': error,
'response': response,
};
}
13 changes: 9 additions & 4 deletions lib/models/geometry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ class Geometry {
required this.type,
});

final List<double> coordinates;
// final List<double> coordinates;
final String type;
final ({double long, double lat}) coordinates;

// (long: coordinates[0], lat: coordinates[1]);

factory Geometry.fromJson(Map<String, dynamic> json) => Geometry(
coordinates:
List<double>.from(json["coordinates"].map((x) => x?.toDouble())),
coordinates: (
long: json["coordinates"][0],
lat: json["coordinates"][1],
),
type: json["type"],
);

Map<String, dynamic> toJson() => {
"coordinates": List<dynamic>.from(coordinates.map((x) => x)),
"coordinates": [coordinates.long, coordinates.lat],
"type": type,
};
}
43 changes: 31 additions & 12 deletions lib/models/location.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
part of mapbox_search;

abstract class Proximity {}
typedef Location = ({double long, double lat});

extension on Location {
String get asString => '$long,$lat';

List<double> get asList => [long, lat];
}

extension on List {
Location get asLocation => (
long: this[0] as double,
lat: this[1] as double,
);
}

sealed class Proximity {
factory Proximity.LocationNone() => const _LocationNone();
factory Proximity.LocationIp() => const _LocationIp();
factory Proximity.Location(Location loc) => _Location(loc: loc);
factory Proximity.LatLong({required double lat, required double long}) =>
_Location(loc: (long: long, lat: lat));
}

/// A class that represents a location around which places will be searched.
class Location implements Proximity {
final double lat;
final double lng;
class _Location implements Proximity {
final Location loc;

const Location({
required this.lat,
required this.lng,
const _Location({
required this.loc,
});

String get asString => '$lng,$lat';
String get asString => loc.asString;
}

/// A class that represents that no location based search will be performed.
class LocationNone implements Proximity {
const LocationNone();
class _LocationNone implements Proximity {
const _LocationNone();
}

/// A class that represents that the location will be based on the IP address of the user.
class LocationIp implements Proximity {
const LocationIp();
class _LocationIp implements Proximity {
const _LocationIp();
}
Loading

0 comments on commit 726ee95

Please sign in to comment.