Can any one explain How to get latitude and longitude values of location using location name
Asked
Active
Viewed 229 times
0
-
3"Geocoding" is the magic word. Should be plenty of resources out there to help you get started. – MH. Oct 21 '13 at 08:46
-
possible duplicate of [How can I find the latitude and longitude from address?](http://stackoverflow.com/questions/3574644/how-can-i-find-the-latitude-and-longitude-from-address) – βhargavḯ Oct 21 '13 at 08:48
4 Answers
0
Use the following code to get lat long values from location name by using reverse geocoding getFromLocationName()
Geocoder coder;
double latitude = 0.0;
double longitude = 0.0;
try { List<Address> address = coder.getFromLocationName(localionNameString, 5);
// if address is found
if (address.size() > 0) {
Address location = address.get(0);
latitude = location.getLatitude();
longitude = location.getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
}
Hope this works

Manishika
- 5,478
- 2
- 22
- 28
0
Its called Geocoding.Assuming the addr is in strAddres, you can use the below logic to get the lat and lon.
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
}

nithinreddy
- 6,167
- 4
- 38
- 44
0
May be my MapHelper
helps you -
public class MapHelper {
public static void setAddress(String address, GoogleMap map){
JSONObject jsonObject = getLocationInfo(address);
Double longitute = getLong(jsonObject);
Double latitude = getLat(jsonObject);
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitute));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
map.clear();
map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitute)).title(address));
}
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ", "%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
public static Double getLong(JSONObject jsonObject) {
Double longitute = 0.0;
try {
longitute = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
} catch (JSONException e) {
}
return longitute;
}
public static Double getLat(JSONObject jsonObject) {
Double latitude = 0.0;
try {
latitude = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
}
return latitude;
}
}
How use:
MapHelper.setAddress("New York", mapFragment.getMap())

jimpanzer
- 3,470
- 4
- 44
- 84