Skip to content

Instantly share code, notes, and snippets.

@prasad091
Created March 27, 2017 09:16
Show Gist options
  • Save prasad091/f160cb18386fb549f651be79d7d65679 to your computer and use it in GitHub Desktop.
Save prasad091/f160cb18386fb549f651be79d7d65679 to your computer and use it in GitHub Desktop.
compile 'com.google.android.gms:play-services:9.6.1'
ArrayList<LatLng> locationLatLong = null;
public void getLocationLatLon(String locationName) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
if (Geocoder.isPresent()) {
try {
Geocoder gc = new Geocoder(this, Locale.getDefault());
List<Address> addresses = gc.getFromLocationName(locationName, 5);
if (addresses != null && addresses.size() > 0) {
locationLatLong = new ArrayList<>(addresses.size());
for (Address a : addresses) {
if (a.hasLatitude() && a.hasLongitude()) {
locationLatLong.add(new LatLng(a.getLatitude(), a.getLongitude()));
}
}
}
} catch (IOException e) {
// handle the exception
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment