在嵌入式地图上居中Google地图标记

时间:2015-04-13 14:22:35

标签: php wordpress google-maps google-maps-api-3 google-maps-markers

无法让这一切为我的生活而工作,我无法弄清楚原因。

好的......

在wordpress帖子上我有一个自定义字段' Post Code'。我已经获取此字段的值的脚本,通过谷歌运行它以获取Lat和Long值并嵌入地图。这就是我所拥有的:

$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>';

为什么它没有将标记置于地图中心?

米罗

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码,似乎工作正常。

一种可能性是http://maps.googleapis.com/maps/api/geocode/xml?address=返回的结果与您的iframe Google地图不同。 (这确实会发生。)

就像@ Dr.Molle所说的那样,除非你想要一个不同的中心,否则你并不需要做中心,因为它默认为q点的中心。