diff --git a/README.md b/README.md index ee353c0a..a023f981 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Location picker using the official [google_maps_flutter](https://pub.dev/packages/google_maps_flutter). -I made This plugin because google deprecated [Place Picker](https://developers.google.com/places/android-sdk/placepicker). +I made This plugin because google deprecated [Place Picker](https://developers.google.com/places/android-sdk/placepicker).

@@ -12,7 +12,6 @@ I made This plugin because google deprecated [Place Picker](https://developers.g [![Demo](https://raw.githubusercontent.com/humazed/google_map_location_picker/master/art/ios_demo.png?raw=true)](https://www.youtube.com/watch?v=Ev1tqijch1o) - ## Using For message localization inside the library please add in `MaterialApp` @@ -35,7 +34,6 @@ MaterialApp( ) ``` - ```dart import 'package:google_map_location_picker/google_map_location_picker.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; @@ -48,6 +46,7 @@ LocationResult result = await showLocationPicker(context, apiKey); Get an API key at . and don't forget to enable in + - Maps SDK for Android - Maps SDK for iOS - Places API @@ -104,6 +103,7 @@ import GoogleMaps } } ``` + Opt-in to the embedded views preview by adding a boolean property to the app's `Info.plist` file with the key `io.flutter.embedded_views_preview` and the value `YES`; you need also to define `NSLocationWhenInUseUsageDescription` @@ -115,6 +115,7 @@ with the key `io.flutter.embedded_views_preview` and the value `YES`; you need a ``` ### Note + The following permissions are not required to use Google Maps Android API v2, but are recommended. `android.permission.ACCESS_COARSE_LOCATION` Allows the API to use WiFi or mobile cell data (or both) to determine the device's location. The API returns the location with an accuracy approximately equivalent to a city block. @@ -122,6 +123,7 @@ The following permissions are not required to use Google Maps Android API v2, bu `android.permission.ACCESS_FINE_LOCATION` Allows the API to determine as precise a location as possible from the available location providers, including the Global Positioning System (GPS) as well as WiFi and mobile cell data. --- + You must also explicitly declare that your app uses the android.hardware.location.network or android.hardware.location.gps hardware features if your app targets Android 5.0 (API level 21) or higher and uses the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to receive location updates from the network or a GPS, respectively. ```xml @@ -130,18 +132,34 @@ You must also explicitly declare that your app uses the android.hardware.locatio ``` --- + The following permissions are defined in the package manifest, and are automatically merged into your app's manifest at build time. You **don't** need to add them explicitly to your manifest: `android.permission.INTERNET` Used by the API to download map tiles from Google Maps servers. `android.permission.ACCESS_NETWORK_STATE` Allows the API to check the connection status in order to determine whether data can be downloaded. +## Restricting Autocomplete Search to Region -### Credits +The `LocationResult`s returned can be restricted to certain countries by passing an array of country codes into the `countries` parameter of `showLocationPicker()`. Countries must be two character, `ISO 3166-1 Alpha-2` compatible. +You can find code information at [Wikipedia: List of ISO 3166 country codes](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes) or the [ISO Online Browsing Platform](https://www.iso.org/obp/ui/#search). + +The example below restricts Autocomplete Search to the United Arab Emirates and Nigeria + +```dart +showLocationPicker( +context, "YOUR API KEY HERE", +initialCenter: LatLng(31.1975844, 29.9598339), +myLocationButtonEnabled: true, +layersButtonEnabled: true, +countries: ['AE', 'NG'], +); +``` + +## Credits The google map from [Flutter's](https://github.com/flutter) [google_maps_flutter](https://pub.dev/packages/google_maps_flutter) package current location and permission from [BaseflowIT's](https://github.com/BaseflowIT) [flutter-geolocator](https://github.com/baseflowit/flutter-geolocator) package. The search bar from [Degreat's](https://github.com/blackmann) [locationpicker](https://github.com/blackmann/locationpicker) package. - diff --git a/example/android/settings_aar.gradle b/example/android/settings_aar.gradle new file mode 100644 index 00000000..e7b4def4 --- /dev/null +++ b/example/android/settings_aar.gradle @@ -0,0 +1 @@ +include ':app' diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh new file mode 100644 index 00000000..d55326f3 --- /dev/null +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# This is a generated file; do not edit or check into version control. +export "FLUTTER_ROOT=C:\Users\Flutter\Source Code\Flutter\SDK\flutter" +export "FLUTTER_APPLICATION_PATH=C:\Users\Flutter\Source Code\google_map_location_picker\example" +export "FLUTTER_TARGET=lib\main.dart" +export "FLUTTER_BUILD_DIR=build" +export "SYMROOT=${SOURCE_ROOT}/../build\ios" +export "OTHER_LDFLAGS=$(inherited) -framework Flutter" +export "FLUTTER_FRAMEWORK_DIR=C:\Users\Flutter\Source Code\Flutter\SDK\flutter\bin\cache\artifacts\engine\ios" +export "FLUTTER_BUILD_NAME=1.0.0" +export "FLUTTER_BUILD_NUMBER=1.0.0" diff --git a/example/lib/main.dart b/example/lib/main.dart index b1c8c83f..ffcbf889 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -48,13 +48,14 @@ class _MyAppState extends State { RaisedButton( onPressed: () async { LocationResult result = await showLocationPicker( - context, - apiKey, + context, apiKey, initialCenter: LatLng(31.1975844, 29.9598339), // automaticallyAnimateToCurrentLocation: true, // mapStylePath: 'assets/mapStyle.json', myLocationButtonEnabled: true, layersButtonEnabled: true, + // countries: ['AE', 'NG'] + // resultCardAlignment: Alignment.bottomCenter, ); print("result = $result"); diff --git a/example/pubspec.lock b/example/pubspec.lock index 93776024..8b2ef70f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -155,7 +155,7 @@ packages: path: ".." relative: true source: path - version: "3.3.2" + version: "3.3.3" google_maps_flutter: dependency: transitive description: diff --git a/lib/src/google_map_location_picker.dart b/lib/src/google_map_location_picker.dart index 87d66d9b..b9e8698e 100644 --- a/lib/src/google_map_location_picker.dart +++ b/lib/src/google_map_location_picker.dart @@ -35,12 +35,14 @@ class LocationPicker extends StatefulWidget { this.resultCardAlignment, this.resultCardDecoration, this.resultCardPadding, + this.countries, }); final String apiKey; final LatLng initialCenter; final double initialZoom; + final List countries; final bool requiredGPS; final bool myLocationButtonEnabled; @@ -144,10 +146,19 @@ class LocationPickerState extends State { /// Fetches the place autocomplete list with the query [place]. void autoCompleteSearch(String place) { place = place.replaceAll(" ", "+"); + + int countriesCount = widget.countries.length; + + String regionParam = widget.countries != null && widget.countries.isNotEmpty + ? "&components=country:${widget.countries.sublist(0, countriesCount > 5 ? 5 : countriesCount).join('|country:')}" + : ""; + + // print('[AutoCompleteSearch] [RegionParam] $regionParam'); + var endpoint = "https://maps.googleapis.com/maps/api/place/autocomplete/json?" + "key=${widget.apiKey}&" + - "input={$place}&sessiontoken=$sessionToken"; + "input={$place}$regionParam&sessiontoken=$sessionToken"; if (locationResult != null) { endpoint += "&location=${locationResult.latLng.latitude}," + @@ -417,6 +428,7 @@ Future showLocationPicker( LatLng initialCenter = const LatLng(45.521563, -122.677433), double initialZoom = 16, bool requiredGPS = true, + List countries, bool myLocationButtonEnabled = false, bool layersButtonEnabled = false, bool automaticallyAnimateToCurrentLocation = true, @@ -432,6 +444,7 @@ Future showLocationPicker( final results = await Navigator.of(context).push( MaterialPageRoute( builder: (BuildContext context) { + // print('[LocationPicker] [countries] ${countries.join(', ')}'); return LocationPicker( apiKey, initialCenter: initialCenter, @@ -449,6 +462,7 @@ Future showLocationPicker( resultCardAlignment: resultCardAlignment, resultCardPadding: resultCardPadding, resultCardDecoration: resultCardDecoration, + countries: countries, ); }, ),