【发布时间】: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¢er=';
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