Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for LatLngBounds #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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()

val 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"]!!));
}

val se = latLng(b["southeast"]!!)
val nw = latLng(b["noethwest"]!!)
val bounds = LatLngBounds(se, nw)
intentBuilder = intentBuilder.setLatLngBounds(bounds);
}
activity.startActivityForResult(intentBuilder.build(activity), PLACE_PICKER_REQUEST)

placeResult = result
Expand Down
14 changes: 12 additions & 2 deletions ios/Classes/SwiftFlutterPlacesDialogPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ init(cont: FlutterViewController) {
result(true)
case "showPlacesPicker":
do {
let config = GMSPlacePickerConfig(viewport: nil)
var viewport: GMSCoordinateBounds? = nil
let bounds = (call.arguments as! [String: Any])["bounds"] as! [String: [String: Double]]?;
if (bounds != nil) {
let northEast = CLLocationCoordinate2D(latitude: bounds!["northeast"]!["latitude"]!,
longitude: bounds!["northeast"]!["longitude"]!)
let southWest = CLLocationCoordinate2D(latitude: bounds!["southwest"]!["latitude"]!,
longitude: bounds!["southwest"]!["longitude"]!)
viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
}

let config = GMSPlacePickerConfig(viewport: viewport)
let placePicker = GMSPlacePickerViewController(config: config)
placePicker.delegate = self
placeResult = result
Expand Down Expand Up @@ -106,4 +116,4 @@ extension SwiftFlutterPlacesDialogPlugin : GMSPlacePickerViewControllerDelegate
viewController.dismiss(animated: true, completion: nil)
placeResult!(nil)
}
}
}
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;
}
}