Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用谷歌地图API将邮政编码转换为纬度/经度坐标?_Javascript_Google Maps_Google Maps Api 3_Geolocation_Yahoo Api - Fatal编程技术网

Javascript 使用谷歌地图API将邮政编码转换为纬度/经度坐标?

Javascript 使用谷歌地图API将邮政编码转换为纬度/经度坐标?,javascript,google-maps,google-maps-api-3,geolocation,yahoo-api,Javascript,Google Maps,Google Maps Api 3,Geolocation,Yahoo Api,我一直在浏览文档和整个网络寻找这个例子,但我找不到这个用例的例子 给定一个邮政编码,我需要以lat/long坐标的形式粗略估计用户的位置。我只有邮政编码,别的什么都没有 这可以通过谷歌地图API实现吗?如果没有,您建议查看哪些其他资源?您可以将任何文本作为地址键传递给地理编码器。如果地理编码成功,您应该会得到一系列结果 从那时起,您可以选择第一个结果或迭代数组。它的每个元素都应该包含一个地址对象,邮政编码是该对象上的一个可能字段 请记住,usps Zipcode不是点或形状。它们是点的数组,使用

我一直在浏览文档和整个网络寻找这个例子,但我找不到这个用例的例子

给定一个邮政编码,我需要以lat/long坐标的形式粗略估计用户的位置。我只有邮政编码,别的什么都没有


这可以通过谷歌地图API实现吗?如果没有,您建议查看哪些其他资源?

您可以将任何文本作为地址键传递给地理编码器。如果地理编码成功,您应该会得到一系列结果

从那时起,您可以选择第一个结果或迭代数组。它的每个元素都应该包含一个地址对象,邮政编码是该对象上的一个可能字段


请记住,usps Zipcode不是点或形状。它们是点的数组,使用这些点作为顶点绘制的多边形的质心可能没有任何特殊意义

也许您可以通过php使用以下脚本来实现这一点:

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];
}
如果通过以下方式调用此函数(*我过滤掉了字符之间的空格,则无需):

然后,您可以将其与您的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'
  });
}
函数初始化(){
var mylatng=newgoogle.maps.LatLng(,);
变量映射选项={
缩放:14,
中心:myLatlng,
disableDefaultUI:true,
滚轮:错误,
}
var map=new google.maps.map(document.getElementById('propMap'),mapOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“地点名称”,
图标:iconBase+'googlemarker.png'
});
}
注意:这对我来说很好,但也许有更好的方法;-)

可能的副本。你要寻找的神奇词语是
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'
  });
}