加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 搜索地址与google maps api v3上最近的现有地标之间的距

发布时间:2020-12-13 22:43:59 所属栏目:PHP教程 来源:网络整理
导读:我正在开发一个包含Google Maps API v3的网页.我目前有一个功能图和搜索栏.我需要能够在地图上的某个KML文件上显示搜索地址到最近地标的距离.我怎样才能做到这一点? 这是页面的代码: script type="text/javascript" var geocoder; var map; var marker; va
我正在开发一个包含Google Maps API v3的网页.我目前有一个功能图和搜索栏.我需要能够在地图上的某个KML文件上显示搜索地址到最近地标的距离.我怎样才能做到这一点?

这是页面的代码:

<script type="text/javascript">
    var geocoder;
    var map; 
    var marker;
    var layers = [];
  function initialize() {
    geocoder = new google.maps.Geocoder ();
    var latlng = new google.maps.LatLng (41,-73.4);
    var myOptions = {
      zoom: 7,center: latlng,mapTypeId: google.maps.MapTypeId.ROADMAP
        }
      map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
      marker = new google.maps.Marker({map:map});


        layers[0] = new google.maps.KmlLayer('http://dl.dropbox.com/u/80233620/South-and-North-County-Trailway.kml',{preserveViewport: true});
        layers[1] = new google.maps.KmlLayer('http://www.nyc.gov/html/dot/downloads/misc/cityracks.kml',{preserveViewport: true});
        layers[2] = new google.maps.KmlLayer('http://dl.dropbox.com/u/80233620/NWS%20Radar%20Images.kmz',{preserveViewport: true});
    for (var i = 0; i < layers.length; i++) {
                layers[i].setMap(map);
              }
        }

    function codeAddress () {
        var address = document.getElementById ("address").value;
        geocoder.geocode ( { 'address': address},function(results,status)  {
        if (status == google.maps.GeocoderStatus.OK)  {
            map.setCenter(results [0].geometry.location);
            marker.setPosition(results [0].geometry.location);
            map.setZoom(14);
            } 
        else {
            alert("Geocode was not successful for the following reason: " + status);
                }
        }); 
                            }
    function toggleLayer(i) {
      if(layers[i].getMap() === null) {
        layers[i].setMap(map);
      }
      else {
        layers[i].setMap(null);}
    }

</script>

解决方法

您无法像这样访问KML图层中的数据

https://developers.google.com/maps/documentation/javascript/layers#KMLLayers

Because KML may include a large number of features,you may not access
feature data from the KmlLayer object directly. Instead,as features
are displayed,they are rendered to look like clickable Maps API
overlays.

相反,您可以处理XML并手动添加标记,然后使用geometry library和computeDistanceBetween()来获取距离.我通常将距离乘以某个数来考虑转弯(距离公式得到直线距离).我相信大约1.2是最准确的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读