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 Commented Dec 5, 2011 at 9:59
  • Yes, also the _supplier_maplatlong would be the map id on the page. Commented 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... Commented Dec 5, 2011 at 12:37
  • Are you getting any errors logged in the console? Commented 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 Commented Dec 5, 2011 at 13:38

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.