Question

I'm building a map that populates its location based on the pages address which changes for each listing. I havent got reverse geocoder but was wondering if there was a way to add just the address instead? if not how can i revers geocode my address? I dont really understand the API pages on google, not up on this kind of thing.

Heres my coding:

      <script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBEbIFpr3iySjmSuJz4mwEomIaXnsA8OdQ&sensor=false">
</script>

            <script>
function initialize()
{
  var mapProp = {
    center: new google.maps.LatLng(51.508742,-0.120850),
    zoom:7,
    disableDefaultUI:true,    
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
        <div id="googleMap" style="width:100%;height:100px;"></div> 
Was it helpful?

Solution

You have to use the Geocoder to get the Latitude and Longitude of a place.

You could do something like this:

var geocoder;
var map;

function initialize()
{
    geocoder = new google.maps.Geocoder();

    var mapProp = {
        center: new google.maps.LatLng(51.508742,-0.120850),
        zoom:7,
        disableDefaultUI:true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    geocoder.geocode({ 'address': 'London' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            // To add a marker:
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

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

All information can be found on https://developers.google.com/maps/documentation/javascript/geocoding

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top