Miro Mitov
Miro Mitov

Reputation: 21

Centre Google Map marker on embeded map

Cannot get this to work for the life of me, and I can't figure out why.

Ok...

On a wordpress post i have a custom field 'Post Code'. The script I have picks up the value of this field, runs it past google to get Lat and Long values and embeds the map. This is what I have:

$postcode = urlencode( get_field("tutor_post_code")); // post code to look up in this case status however can easily be retrieved from a database or a form post
    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=true"; // the request URL you'll send to google to get back your XML feed
    $xml = simplexml_load_file($request_url) or die("url not loading");// XML request
    $status = $xml->status;// GET the request status as google's api can return several responses
    if ($status=="OK") {
        //request returned completed time to get lat / lang for storage
        $lat = $xml->result->geometry->location->lat;
        $long = $xml->result->geometry->location->lng;
        echo "$lat,$long";  //spit out results or you can store them in a DB if you wish
    }
    if ($status=="ZERO_RESULTS") {
        //indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
    }
    if ($status=="OVER_QUERY_LIMIT") {
        //indicates that you are over your quota of geocode requests against the google api
    }
    if ($status=="REQUEST_DENIED") {
        //indicates that your request was denied, generally because of lack of a sensor parameter.
    }
    if ($status=="INVALID_REQUEST") {
        //generally indicates that the query (address or latlng) is missing.
    }


    echo '<iframe width="100%" height="400" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=';
    echo get_field("tutor_post_code");
    echo '&zoom=13&center=';
    echo "$lat,$long";
    echo '&key=AIzaSyB8LLFEJV_Or1sj_u1PGKw12n6leDKND3o"></iframe>';

Any ideas why it's not centering the marker on the map?

Miro

Upvotes: 0

Views: 131

Answers (1)

kaho
kaho

Reputation: 4784

I tried your code and it seems to be working fine.

One possibility is http://maps.googleapis.com/maps/api/geocode/xml?address=is returning a different result than your iframe Google Maps. (This does happen sometime.)

Like @Dr.Molle said, you don't really need to do center as it is default to center to the q point, unless you wants a different center.

Upvotes: 1

Related Questions