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
19 changes: 11 additions & 8 deletions android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
Expand Down Expand Up @@ -31,11 +32,11 @@ class NotAvailableException extends Exception {
*/
public class GeocoderPlugin implements MethodCallHandler {

private Geocoder geocoder;
private Context context;

public GeocoderPlugin(Context context) {

this.geocoder = new Geocoder(context);
this.context = context;
}

/**
Expand Down Expand Up @@ -102,26 +103,27 @@ public void onMethodCall(MethodCall call, Result rawResult) {
else if (call.method.equals("findAddressesFromCoordinates")) {
float latitude = ((Number) call.argument("latitude")).floatValue();
float longitude = ((Number) call.argument("longitude")).floatValue();
findAddressesFromCoordinates(latitude,longitude, result);
String language = ((String) call.argument("language"));
findAddressesFromCoordinates(latitude, longitude, language, result);
} else {
result.notImplemented();
}
}

private void assertPresent() throws NotAvailableException {
private void assertPresent(Geocoder geocoder) throws NotAvailableException {
if (!geocoder.isPresent()) {
throw new NotAvailableException();
}
}

private void findAddressesFromQuery(final String address, final Result result) {

final GeocoderPlugin plugin = this;
final Geocoder geocoder = new Geocoder(context);
new AsyncTask<Void, Void, List<Address>>() {
@Override
protected List<Address> doInBackground(Void... params) {
try {
plugin.assertPresent();
plugin.assertPresent(geocoder);
return geocoder.getFromLocationName(address, 20);
} catch (IOException ex) {
return null;
Expand All @@ -143,13 +145,14 @@ protected void onPostExecute(List<Address> addresses) {
}.execute();
}

private void findAddressesFromCoordinates(final float latitude, final float longitude, final Result result) {
private void findAddressesFromCoordinates(final float latitude, final float longitude, final String language, final Result result) {
final GeocoderPlugin plugin = this;
final Geocoder geocoder = language == null ? new Geocoder(context) : new Geocoder(context, new Locale(language));
new AsyncTask<Void, Void, List<Address>>() {
@Override
protected List<Address> doInBackground(Void... params) {
try {
plugin.assertPresent();
plugin.assertPresent(geocoder);
return geocoder.getFromLocation(latitude, longitude, 20);
} catch (IOException ex) {
return null;
Expand Down
23 changes: 17 additions & 6 deletions ios/Classes/GeocoderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,25 @@ - (void) handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result
if ([@"findAddressesFromCoordinates" isEqualToString:call.method]) {
NSNumber *longitude = call.arguments[@"longitude"];
NSNumber *latitude = call.arguments[@"latitude"];
NSString *language = call.arguments[@"language"];
CLLocation * location = [[CLLocation alloc] initWithLatitude:latitude.doubleValue longitude:longitude.doubleValue];
[self initializeGeocoder];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
return result(error.flutterError);
}
result([self placemarksToDictionary:placemarks]);
}];
if (language != (id)[NSNull null] && @available(iOS 11.0, *)) {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:language];
[self.geocoder reverseGeocodeLocation:location preferredLocale:locale completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
return result(error.flutterError);
}
result([self placemarksToDictionary:placemarks]);
}];
} else {
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
return result(error.flutterError);
}
result([self placemarksToDictionary:placemarks]);
}];
}
}
else if ([@"findAddressesFromQuery" isEqualToString:call.method]) {
NSString *address = call.arguments[@"address"];
Expand Down
1 change: 1 addition & 0 deletions lib/geocoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export 'model.dart';

class Geocoder {
static final Geocoding local = LocalGeocoding();
static Geocoding withLanguage(String language) => LocalGeocoding(language: language);
static Geocoding google(String apiKey, { String language }) => GoogleGeocoding(apiKey, language: language);
}
10 changes: 9 additions & 1 deletion lib/services/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import 'package:geocoder/services/base.dart';

/// Geocoding and reverse geocoding through built-lin local platform services.
class LocalGeocoding implements Geocoding {
final String language;

LocalGeocoding({ this.language });

static const MethodChannel _channel = MethodChannel('github.com/aloisdeniel/geocoder');

Future<List<Address>> findAddressesFromCoordinates(Coordinates coordinates) async {
Iterable addresses = await _channel.invokeMethod('findAddressesFromCoordinates', coordinates.toMap());
Iterable addresses = await _channel.invokeMethod('findAddressesFromCoordinates', {
"latitude": coordinates.latitude,
"longitude": coordinates.longitude,
"language": language,
});
return addresses.map((x) => Address.fromMap(x)).toList();
}

Expand Down