From f336c3d25d4c31cc7127c9af4179e87dd1fb8fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 12 Oct 2018 07:21:52 +0900 Subject: [PATCH] Add support for LatLngBounds --- .../FlutterPlacesDialogPlugin.kt | 14 ++++ lib/flutter_places_dialog.dart | 77 +++++++++++-------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/android/src/main/kotlin/com/example/flutterplacesdialog/FlutterPlacesDialogPlugin.kt b/android/src/main/kotlin/com/example/flutterplacesdialog/FlutterPlacesDialogPlugin.kt index c5e4889..00a5900 100644 --- a/android/src/main/kotlin/com/example/flutterplacesdialog/FlutterPlacesDialogPlugin.kt +++ b/android/src/main/kotlin/com/example/flutterplacesdialog/FlutterPlacesDialogPlugin.kt @@ -13,6 +13,8 @@ import com.google.android.gms.location.places.ui.PlacePicker import com.google.android.gms.location.places.ui.PlaceAutocomplete import com.google.android.gms.location.places.Place import com.google.android.gms.common.GoogleApiAvailability +import com.google.android.gms.maps.model.LatLng +import com.google.android.gms.maps.model.LatLngBounds class FlutterPlacesDialogPlugin(val activity: Activity) : MethodCallHandler, io.flutter.plugin.common.PluginRegistry.ActivityResultListener { var placeResult: Result? = null @@ -41,6 +43,18 @@ class FlutterPlacesDialogPlugin(val activity: Activity) : MethodCallHandler, io. } var intentBuilder = PlacePicker.IntentBuilder() + + var b = call.argument>>("bounds"); + if (b != null) { + fun latLng(m: Map): LatLng { + return LatLng(m["latitude"]!!, (m["longitude"]!!)); + } + + var se = latLng(b["southeast"]!!) + var nw = latLng(b["noethwest"]!!) + var bounds = LatLngBounds(se, nw); + intentBuilder = intentBuilder.setLatLngBounds(bounds); + } activity.startActivityForResult(intentBuilder.build(activity), PLACE_PICKER_REQUEST) placeResult = result diff --git a/lib/flutter_places_dialog.dart b/lib/flutter_places_dialog.dart index ff5a984..a926241 100644 --- a/lib/flutter_places_dialog.dart +++ b/lib/flutter_places_dialog.dart @@ -1,26 +1,48 @@ import 'dart:async'; import 'package:flutter/services.dart'; +import 'package:meta/meta.dart'; -enum PriceLevel { - Unknown, - Free, - Cheap, - Medium, - High, - Expensive -} +enum PriceLevel { Unknown, Free, Cheap, Medium, High, Expensive } class PlaceLatLong { - PlaceLatLong({this.latitude, this.longitude}); - num latitude; - num longitude; + const PlaceLatLong({ + @required this.latitude, + @required this.longitude, + }); + final double latitude; + final double longitude; + + PlaceLatLong.fromJson(Map data) + : this( + latitude: data['latitude'], + longitude: data['longitude'], + ); + + Map toJson() => { + 'latitude': latitude, + 'longitude': longitude, + }; } class PlaceBounds { - PlaceBounds({this.northeast, this.southwest}); - PlaceLatLong northeast; - PlaceLatLong southwest; + const PlaceBounds({ + @required this.northeast, + @required this.southwest, + }); + final PlaceLatLong northeast; + final PlaceLatLong southwest; + + PlaceBounds.fromJson(Map data) + : this( + northeast: PlaceLatLong.fromJson(data['northeast']), + southwest: PlaceLatLong.fromJson(data['southwest']), + ); + + Map toJson() => { + 'northeast': northeast.toJson(), + 'southwest': southwest.toJson(), + }; } class PlaceDetails { @@ -58,24 +80,27 @@ class FlutterPlacesDialog { return ret; } - static Future getPlacesDialog() async { + static Future getPlacesDialog({ + PlaceBounds bounds, + }) async { print('Opening places dialog'); - Map data = await _channel.invokeMethod("showPlacesPicker"); + Map data = + await _channel.invokeMethod("showPlacesPicker", { + "bounds": bounds?.toJson(), + }); print("Places data $data"); PlaceDetails details = new PlaceDetails(); details.name = data["name"]; details.address = data["address"]; details.placeid = data["placeid"]; - details.location = new PlaceLatLong(); - details.location.longitude = data["longitude"]; - details.location.latitude = data["latitude"]; + details.location = new PlaceLatLong.fromJson(data); details.phoneNumber = data["phoneNumber"]; switch (data["priceLevel"]) { case -1: details.priceLevel = PriceLevel.Unknown; break; case 0: - details.priceLevel= PriceLevel.Free; + details.priceLevel = PriceLevel.Free; break; case 1: details.priceLevel = PriceLevel.Cheap; @@ -84,7 +109,7 @@ class FlutterPlacesDialog { details.priceLevel = PriceLevel.Medium; break; case 3: - details.priceLevel= PriceLevel.High; + details.priceLevel = PriceLevel.High; break; case 4: details.priceLevel = PriceLevel.Expensive; @@ -94,15 +119,7 @@ class FlutterPlacesDialog { break; } details.rating = data["rating"]; - details.bounds = new PlaceBounds(); - details.bounds.northeast = new PlaceLatLong(); - details.bounds.northeast.latitude = data["bounds"]["northeast"]["latitude"]; - details.bounds.northeast.latitude = - data["bounds"]["northeast"]["longitude"]; - details.bounds.southwest = new PlaceLatLong(); - details.bounds.northeast.latitude = data["bounds"]["southwest"]["latitude"]; - details.bounds.northeast.latitude = - data["bounds"]["southwest"]["longitude"]; + details.bounds = new PlaceBounds.fromJson(data['bounds']); return details; } }