【问题标题】:Centre Google Map marker on embeded map在嵌入式地图上居中 Google 地图标记
【发布时间】:2015-06-18 21:43:15
【问题描述】:

无法让它为我的一生工作,我不知道为什么。

好的……

在 wordpress 帖子中,我有一个自定义字段“邮政编码”。我的脚本获取了该字段的值,通过 google 运行它以获取 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>';

任何想法为什么它没有在地图上居中标记?

米罗

【问题讨论】:

  • 当您使用邮政编码为您的 iframe 生成 url 时,您是否尝试过使用 urlencode?您在顶部执行此操作,因此它已经在一个变量中,然后在底部您只是回显该值,我不确定您在其中有什么,因此可能会出现问题。您生成的 iframe 代码是什么样的?发布输出
  • 移除中心参数。基本上地理编码是多余的,你只需要 q 参数(找到地址/地点后地图将自动居中)
  • 嗨,根据谷歌的说法,中心参数用于使地图居中。它也只适用于经纬度值,因此我正在对上面的邮政编码进行地理编码以获取它。它肯定会返回正确的坐标。地图指针始终位于 iframe 的左上角...link

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


【解决方案1】:

我试过你的代码,它似乎工作正常。

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

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

【讨论】:

  • 嗨,如前所述,根据谷歌的说法,需要中心参数来使地图居中,并且只需要经纬度坐标,因此我正在对邮政编码进行地理编码以获取它们。还是行不通。删除 center 参数不起作用。
  • 您尝试的输入是什么..?
  • 输入的是邮政编码。然后它返回纬度和经度坐标。
猜你喜欢
  • 2017-04-11
  • 2014-11-07
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 2019-08-01
  • 2023-04-10
  • 2012-06-29
  • 1970-01-01
相关资源
最近更新 更多