0

Is there an easy way to plot a location on a Google map using an address specified in a custom field?

For example, if a post has the following address specified;

1600 Pennsylvania Avenue, NW Washington, DC 20500

A Google map will automatically be displayed on the page with with a marker on this exact location?

Surely it can't be that difficult.

I've looked at the API but nothing sprung out at me

1 Answer 1

3

Dean, your best bet is to do it through JS on the front end.

I use the following function to covert the postcode to a Lat and Lng:

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

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });

            markersArray.push(marker);      

            // BA Lng, za Lat 

            //console.log(results[0].geometry.location['Ba']);

            document.getElementById("_supplier_maplatlong").value = results[0].geometry.location;

        }else{
            alert("Postcode not found");
        }
    });
}

Then just drop in the postcode in to a hidden input or something and do something along the lines of:

if($('#_supplier_postcode').val()){
        codeAddress($('#_supplier_postcode').val());
    }

Once you've initialize() obviously.

7
  • I'm guessing that #_supplier_postcode would be the custom field name? Sorry, jQuery really isn't my strong suit Dec 5, 2011 at 9:59
  • Yes, also the _supplier_maplatlong would be the map id on the page.
    – Alex Older
    Dec 5, 2011 at 11:11
  • I've tried using this method but I keep getting errors (Or more specifically, the page doesn't even appear). I'm clearly doing something wrong somewhere... Dec 5, 2011 at 12:37
  • Are you getting any errors logged in the console?
    – Alex Older
    Dec 5, 2011 at 13:15
  • Nope, it's one of those errors where no content loads at all. Viewing the source code just brings up a blank page Dec 5, 2011 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.