Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=C:\flutter"
export "FLUTTER_APPLICATION_PATH=C:\Users\PC\Desktop\google_map_location_picker\example"
export "FLUTTER_TARGET=lib\main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build\ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1.0.0"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=false"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.packages"
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:google_map_location_picker/generated/l10n.dart'
as location_picker;
import 'package:google_map_location_picker/google_map_location_picker.dart';
import 'package:google_map_location_picker_example/keys.dart';
//import 'package:google_map_location_picker_example/keys.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import 'generated/i18n.dart';
Expand Down Expand Up @@ -52,7 +52,7 @@ class _MyAppState extends State<MyApp> {
onPressed: () async {
LocationResult result = await showLocationPicker(
context,
apiKey,
"",
initialCenter: LatLng(31.1975844, 29.9598339),
// automaticallyAnimateToCurrentLocation: true,
// mapStylePath: 'assets/mapStyle.json',
Expand Down
26 changes: 20 additions & 6 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,28 @@ packages:
name: geolocator
url: "https://pub.dartlang.org"
source: hosted
version: "6.2.1"
version: "7.0.3"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.9"
version: "2.0.2"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
version: "2.0.3"
google_map_location_picker:
dependency: "direct dev"
description:
path: ".."
relative: true
source: path
version: "4.1.6"
version: "4.1.7"
google_maps_flutter:
dependency: transitive
description:
Expand All @@ -133,6 +133,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
google_maps_webservice:
dependency: transitive
description:
name: google_maps_webservice
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.20-nullsafety.5"
http:
dependency: transitive
description:
Expand Down Expand Up @@ -161,6 +168,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.1"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -216,7 +230,7 @@ packages:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
version: "2.0.0"
provider:
dependency: transitive
description:
Expand Down Expand Up @@ -293,5 +307,5 @@ packages:
source: hosted
version: "2.1.0"
sdks:
dart: ">=2.12.0-259.9.beta <3.0.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=2.0.0"
83 changes: 47 additions & 36 deletions lib/src/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import 'package:google_map_location_picker/src/providers/location_provider.dart'
import 'package:google_map_location_picker/src/utils/loading_builder.dart';
import 'package:google_map_location_picker/src/utils/log.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:google_maps_webservice/geocoding.dart';
import 'package:google_maps_webservice/places.dart';
import 'package:provider/provider.dart';

import 'model/location_result.dart';
import 'utils/location_utils.dart';

class MapPicker extends StatefulWidget {
const MapPicker(
Expand Down Expand Up @@ -79,9 +79,7 @@ class MapPickerState extends State<MapPicker> {

Position _currentPosition;

String _address;

String _placeId;
LocationResult _locationResult;

void _onToggleMapTypePressed() {
final MapType nextType =
Expand All @@ -94,8 +92,8 @@ class MapPickerState extends State<MapPicker> {
Future<void> _initCurrentLocation() async {
Position currentPosition;
try {
currentPosition =
await getCurrentPosition(desiredAccuracy: widget.desiredAccuracy);
currentPosition = await Geolocator.getCurrentPosition(
desiredAccuracy: widget.desiredAccuracy);
d("position = $currentPosition");

setState(() => _currentPosition = currentPosition);
Expand Down Expand Up @@ -226,7 +224,7 @@ class MapPickerState extends State<MapPicker> {
children: <Widget>[
Flexible(
flex: 20,
child: FutureLoadingBuilder<Map<String, String>>(
child: FutureLoadingBuilder<LocationResult>(
future: getAddress(locationProvider.lastIdleLocation),
mutable: true,
loadingIndicator: Row(
Expand All @@ -236,10 +234,9 @@ class MapPickerState extends State<MapPicker> {
],
),
builder: (context, data) {
_address = data["address"];
_placeId = data["placeId"];
_locationResult = data;
return Text(
_address ??
data?.address ??
S.of(context)?.unnamedPlace ??
'Unnamed place',
style: TextStyle(fontSize: 18),
Expand All @@ -249,14 +246,13 @@ class MapPickerState extends State<MapPicker> {
),
Spacer(),
FloatingActionButton(
onPressed: () {
Navigator.of(context).pop({
'location': LocationResult(
latLng: locationProvider.lastIdleLocation,
address: _address,
placeId: _placeId,
)
});
onPressed: () async {
final places =
new GoogleMapsPlaces(apiKey: widget.apiKey);
final placeDetails = await places
.getDetailsByPlaceId(_locationResult.placeId);
_locationResult.placeDetails = placeDetails.result;
Navigator.of(context).pop({'location': _locationResult});
},
child: widget.resultCardConfirmIcon ??
Icon(Icons.arrow_forward),
Expand All @@ -270,25 +266,40 @@ class MapPickerState extends State<MapPicker> {
);
}

Future<Map<String, String>> getAddress(LatLng location) async {
Future<LocationResult> getAddress(LatLng location) async {
try {
final endpoint =
'https://maps.googleapis.com/maps/api/geocode/json?latlng=${location?.latitude},${location?.longitude}'
'&key=${widget.apiKey}&language=${widget.language}';

final response = jsonDecode((await http.get(Uri.parse(endpoint),
headers: await LocationUtils.getAppHeaders()))
.body);

return {
"placeId": response['results'][0]['place_id'],
"address": response['results'][0]['formatted_address']
};
final googleMapsGeocoding =
new GoogleMapsGeocoding(apiKey: widget.apiKey);
final geocodingResponse = await googleMapsGeocoding.searchByLocation(
Location(lat: location?.latitude, lng: location?.longitude),
language: widget.language);
final results = geocodingResponse.results;
return LocationResult(
address: results[0].formattedAddress,
placeId: results[0].placeId,
country: extractCountryName(results[0]),
city: extractCityName(results[0]));
} catch (e) {
print(e);
}

return {"placeId": null, "address": null};
return LocationResult();
}

String extractCountryName(GeocodingResult result) {
return result.addressComponents
.firstWhere((address) => address.types.contains("country"),
orElse: () =>
AddressComponent(types: [], longName: "", shortName: ""))
.longName;
}

String extractCityName(GeocodingResult result) {
return result.addressComponents
.firstWhere((address) => address.types.contains("locality"),
orElse: () =>
AddressComponent(types: [], longName: "", shortName: ""))
.longName;
}

Widget pin() {
Expand Down Expand Up @@ -324,7 +335,7 @@ class MapPickerState extends State<MapPicker> {
var dialogOpen;

Future _checkGeolocationPermission() async {
final geolocationStatus = await checkPermission();
final geolocationStatus = await Geolocator.checkPermission();
d("geolocationStatus = $geolocationStatus");

if (geolocationStatus == LocationPermission.denied && dialogOpen == null) {
Expand Down Expand Up @@ -399,7 +410,7 @@ class MapPickerState extends State<MapPicker> {
child: Text(S.of(context)?.ok ?? 'Ok'),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
openAppSettings();
Geolocator.openAppSettings();
dialogOpen = null;
},
),
Expand All @@ -412,7 +423,7 @@ class MapPickerState extends State<MapPicker> {

// TODO: 9/12/2020 this is no longer needed, remove in the next release
Future _checkGps() async {
if (!(await isLocationServiceEnabled())) {
if (!(await Geolocator.isLocationServiceEnabled())) {
if (Theme.of(context).platform == TargetPlatform.android) {
showDialog(
context: context,
Expand Down
41 changes: 39 additions & 2 deletions lib/src/model/location_result.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_webservice/places.dart';

/// The result returned after completing location selection.
class LocationResult {
Expand All @@ -13,10 +14,46 @@ class LocationResult {
/// Latitude/Longitude of the selected location.
LatLng latLng;

LocationResult({this.latLng, this.address, this.placeId});
/// google map city
String city;

/// google map country
String country;

/// place details
PlaceDetails placeDetails;

LocationResult(
{this.latLng,
this.address,
this.placeId,
this.country,
this.city,
this.placeDetails});

@override
String toString() {
return 'LocationResult{address: $address, latLng: $latLng, placeId: $placeId}';
return 'LocationResult{address: $address, latLng: $latLng, placeId: $placeId, country: $country, city: $city}';
}

static LocationResult fromMap(Map<dynamic, dynamic> json) {
return LocationResult(
latLng: LatLng.fromJson(json["latLng"]),
city: json["city"],
country: json["country"],
address: json["address"],
placeId: json["placeId"],
placeDetails: PlaceDetails.fromJson(json["placeDetails"]));
}

Map<dynamic, dynamic> toJson() {
return {
"address": address,
"placeId": placeId,
"latLng": latLng?.toJson(),
"city": city,
"country": country,
"placeDetails": placeDetails?.toJson(),
};
}
}
26 changes: 20 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ packages:
name: geolocator
url: "https://pub.dartlang.org"
source: hosted
version: "6.2.1"
version: "7.0.3"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.9"
version: "2.0.2"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
version: "2.0.3"
google_maps_flutter:
dependency: "direct main"
description:
Expand All @@ -119,8 +119,15 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
http:
google_maps_webservice:
dependency: "direct main"
description:
name: google_maps_webservice
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.20-nullsafety.5"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
Expand All @@ -147,6 +154,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.1"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -202,7 +216,7 @@ packages:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
version: "2.0.0"
provider:
dependency: "direct main"
description:
Expand Down Expand Up @@ -279,5 +293,5 @@ packages:
source: hosted
version: "2.1.0"
sdks:
dart: ">=2.12.0-259.9.beta <3.0.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=2.0.0"
Loading