Muhammad Nadeem
Muhammad Nadeem

Reputation: 181

I want to get the lat-long of a city name

I want to move the google map to a city. The user can select the city name from the select box. I am required the lat-long of the selected city. When user select the city i have to find its lat-long. I am using this code to move the map

var center = new google.maps.LatLng(lat,long);
map.panTo(center);

Upvotes: 2

Views: 95

Answers (2)

Muhammad Nadeem
Muhammad Nadeem

Reputation: 181

I tried it with below code

var address = city_name;
geocoder.geocode({'address': address},function(results, status){
    if (status === google.maps.GeocoderStatus.OK)
    {
        map.setCenter(results[0].geometry.location);
    }else
    {
        alert('Geocode was not successful for the following reason: ' + status);
    }
});

Upvotes: 3

aprok
aprok

Reputation: 1157

You have to use Google geocoder. It's API is here. With it's help you can take lat-long like so:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({
    address: "Your address here"
}, function(results, status) {
    // here you get your result
});

Upvotes: 0

Related Questions