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>

If you want more examples for the Javascript API, try this link : https://developers.google.com/maps/documentation/javascript/examples/

The code I wrote is inspired from the geocoding-simple sample.

Regards.

EDIT 1:

You can achieve it using an non-official PHP library. Check this example : http://www.bradwedell.com/phpgooglemapapi/demos/geocoding.php (The code is at the bottom=


Update per comment below: Doesn't work after July 2018.


This seems needlessly complicated. Here's an example "nearby events" map. It will take City, States, convert them to latLng coords, and put markers on a map:

// Nearby Events with Google Maps
window.nearbyEventsMap = () => {
    const centerOfUS = {
        lat: 37.09024,
        lng: -95.712891
    }

    // Create a map object and specify the DOM element for display.
    const map = new google.maps.Map(document.querySelector('#nearby_events_map'), {
        center: centerOfUS,
        scrollwheel: false,
        zoom: 4
    })

    // Create a marker and set its position.
    const geocoder = new google.maps.Geocoder()

    // Filter out duplicate cityStates
    let cityStates = {}
    document.querySelectorAll('.nearby_event_city_state').forEach(event => {
        cityStates[event] = event.innerText
    })

    // `cityState` is in the format of "City, State". It's not picky about state being a word or the abbreviation.
    for (const cityState in cityStates) {
        const location = cityStates[cityState]

        geocoder.geocode({
            address: location
        }, function (results, status) {
            if (status === 'OK') {
                const result = results[0].geometry.location
                const lat = result.lat()
                const lng = result.lng()
                const latLng = {
                    lat,
                    lng
                }

                return new google.maps.Marker({
                    map: map,
                    position: latLng
                })
            }
        })
    }
}
// /Nearby Events with Google Maps

Make sure to include your <script> tags.

<script src="/dist/js/main.js"></script>

<!-- We're loading this after the main.js script so the callback from main.js will be available to it. -->
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=nearbyEventsMap"></script>

StackOverflow please join everyone else and get GitHub markdown with syntax highlighting...


You can use Google's geocoding service, e.g.,

http://maps.googleapis.com/maps/api/geocode/xml?address=Miami+FL&sensor=false

That gives you back georeferenced data in a variety of formats (JSON, XML, etc). In any event, the location is definitely in the returned data block.

The API docs are at:

https://developers.google.com/maps/documentation/geocoding/