我一直在文档中和整个网络中寻找一个示例,但找不到该用例的示例。
给出邮政编码,我需要以纬度/经度坐标的形式粗略估计用户的位置。我所拥有的只是邮政编码,仅此而已。
可以使用Google Maps API完成吗?如果没有,您建议您考虑使用哪些其他资源?
请记住,usps邮政编码既不是点也不是形状。它们是点的数组,使用这些点作为顶点绘制的多边形的质心可能没有任何特殊含义。
function getLnt($zip){
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false";
$result_string = file_get_contents($url);
$result = json_decode($result_string, true);
$result1[]=$result['results'][0];
$result2[]=$result1[0]['geometry'];
$result3[]=$result2[0]['location'];
return $result3[0];
}
如果您通过以下方式调用此函数(*我过滤掉了字符之间的空格,则没有必要):
$coords = str_replace(' ','',$foo->zipcode);*
$val = getLnt($coords);
/* print latitude & longitude for example */
print $val['lat'];
print $val['lng'];
然后,您可以将其与您的Google地图脚本混合,例如:
function initialize(){
var myLatlng = new google.maps.LatLng(<?= $val['lat'] ?>,<?= $val['lng'] ?>);
var mapOptions = {
zoom: 14,
center: myLatlng,
disableDefaultUI: true,
scrollwheel: false,
}
var map = new google.maps.Map(document.getElementById('propMap'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'title of location',
icon: iconBase + 'google-marker.png'
});
}
注意:这对我来说很好,但是也许有更好的方法可以做到这一点;-)