Skip to content

Commit

Permalink
Add support for LatLngBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 11, 2018
1 parent 5fb4a7e commit f336c3d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,6 +43,18 @@ class FlutterPlacesDialogPlugin(val activity: Activity) : MethodCallHandler, io.
}

var intentBuilder = PlacePicker.IntentBuilder()

var b = call.argument<Map<String, Map<String, Double>>>("bounds");
if (b != null) {
fun latLng(m: Map<String, Double>): 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
Expand Down
77 changes: 47 additions & 30 deletions lib/flutter_places_dialog.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> data)
: this(
latitude: data['latitude'],
longitude: data['longitude'],
);

Map<String, dynamic> 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<String, dynamic> data)
: this(
northeast: PlaceLatLong.fromJson(data['northeast']),
southwest: PlaceLatLong.fromJson(data['southwest']),
);

Map<String, dynamic> toJson() => {
'northeast': northeast.toJson(),
'southwest': southwest.toJson(),
};
}

class PlaceDetails {
Expand Down Expand Up @@ -58,24 +80,27 @@ class FlutterPlacesDialog {
return ret;
}

static Future<PlaceDetails> getPlacesDialog() async {
static Future<PlaceDetails> getPlacesDialog({
PlaceBounds bounds,
}) async {
print('Opening places dialog');
Map<dynamic, dynamic> data = await _channel.invokeMethod("showPlacesPicker");
Map<dynamic, dynamic> 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;
Expand All @@ -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;
Expand All @@ -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;
}
}

0 comments on commit f336c3d

Please sign in to comment.