How to Get City Name from Latitude and Longitude Coordinates in Google Maps

How to get city name from latitude and longitude coordinates in Google Maps?

From a Geocoder object, you can call the getFromLocation(double, double, int) method. It will return a list of Address objects that have a method getLocality().

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
}
else {
// do your stuff
}

how to get city name using latitude and longitude in android

Try

Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
String locality = addresses.get(0).getLocality();
}

How can I get city name from a latitude and longitude point?

This is called Reverse Geocoding

  • Documentation from Google:

    http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding.

  • Sample Call to Google's geocode Web Service:

    http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true&key=YOUR_KEY

How to get city name from latitude and longitude in phone gap?

var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);

geocoder.geocode(
{'latLng': latlng},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var add= results[0].formatted_address ;
var value=add.split(",");

count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
alert("city name is: " + city);
}
else {
alert("address not found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
}
);

Split the full address with "," as delimiter and get the city name..

Getting coordinates based on city name google map

The geocoder is asynchronous. You need to use the returned data in the callback function when/where it is available.

related question: Using Address Instead Of Longitude And Latitude With Google Maps API

proof of concept fiddle

code snippet:

function initialize() {

var address = 'Zurich, Ch';

var geocoder = new google.maps.Geocoder();

geocoder.geocode({

'address': address

}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) {

var Lat = results[0].geometry.location.lat();

var Lng = results[0].geometry.location.lng();

var myOptions = {

zoom: 11,

center: new google.maps.LatLng(Lat, Lng)

};

var map = new google.maps.Map(

document.getElementById("map_canvas"), myOptions);

} else {

alert("Something got wrong " + status);

}

});

}

google.maps.event.addDomListener(window, "load", initialize);
html,

body,

#map_canvas {

height: 100%;

width: 100%;

margin: 0px;

padding: 0px

}
<script src="https://maps.googleapis.com/maps/api/js"></script>

<div id="map_canvas"></div>

get city name, address using latitude and longitude in appcelerator

I would suggest firstly using different variables for your return parameters in the functions and use negative conditions to reduce indentation:

Titanium.Geolocation.getCurrentPosition(function(position) {

if (!position.success || position.error) {
alert('Could not find the device location');
return;
}
longitude = position.coords.longitude; // -88.0747875
latitude = position.coords.latitude; // 41.801141

Titanium.Geolocation.reverseGeocoder(latitude, longitude, function(result) {
if (!result.success || !result.places || result.places.length == 0) {
alert('Could not find any places.');
return;
}
var places = result.places;
Ti.API.info("\nReverse Geocode address == " + JSON.stringify(places));
driverCity = places[0].city;
// Current city
driverState = places[0].address;
// Current State
annotation.title = places[0].displayAddress;
// Whole address
// Ti.API.info("\nReverse Geocode address == " + JSON.stringify(places));
});
});

Then see what is being returned from your calls.

Google Maps: how to get country, state/province/region, city given a lat/long value?

What you are looking for is called reverse geocoding. Google provides a server-side reverse geocoding service through the Google Geocoding API, which you should be able to use for your project.

This is how a response to the following request would look like:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

Response:

{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
"address_components": [ {
"long_name": "275-291",
"short_name": "275-291",
"types": [ "street_number" ]
}, {
"long_name": "Bedford Ave",
"short_name": "Bedford Ave",
"types": [ "route" ]
}, {
"long_name": "New York",
"short_name": "New York",
"types": [ "locality", "political" ]
}, {
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "Kings",
"short_name": "Kings",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "New York",
"short_name": "NY",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "11211",
"short_name": "11211",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 40.7142298,
"lng": -73.9614669
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"southwest": {
"lat": 40.7110822,
"lng": -73.9646145
},
"northeast": {
"lat": 40.7173774,
"lng": -73.9583193
}
}
}
},

... Additional results[] ...

You can also opt to receive the response in xml instead of json, by simply substituting json for xml in the request URI:

http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

As far as I know, Google will also return the same name for address components, especially for high-level names like country names and city names. Nevertheless, keep in mind that while the results are very accurate for most applications, you could still find the occasional spelling mistake or ambiguous result.

Google maps get latitude and longitude having city name?

You can find the code jsfiddled here : http://jsfiddle.net/YphZw/
or below :

$("#btn").click(function(){

var geocoder = new google.maps.Geocoder();

geocoder.geocode( { 'address': 'miami, us'}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) {

alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng());

} else {

alert("Something got wrong " + status);

}

});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<html>

<head>

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

</head>

<body>

<input id="btn" type="button" value="search for miami coordinates" />

</body>

</html>


Related Topics



Leave a reply



Submit