【问题标题】:Distance between a searched address and nearest existing placemark on a google maps api v3google maps api v3上搜索到的地址与最近的现有地标之间的距离
【发布时间】:2012-06-05 03:32:14
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: php javascript google-maps google-maps-api-3 distance


    【解决方案1】:

    您不能像那样访问 KML 图层中的数据

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

    由于 KML 可能包含大量功能,您可能无法访问 直接来自 KmlLayer 对象的特征数据。相反,作为特征 被显示,它们被渲染成看起来像可点击的 Maps API 覆盖。

    相反,您可以手动处理 XML 并添加标记,然后使用 geometry librarycomputeDistanceBetween() 获取距离。我通常将距离乘以某个数字来计算转弯(距离公式得到直线距离)。我相信1.2 附近是最准确的。

    【讨论】:

    • 处理 XML 并手动添加标记是什么意思?
    • 您必须使用 php 打开 xml 文件并提取点并添加标记。 Google 地图不允许您通过 kml 图层访问该数据。
    • 我不熟悉php,如何用它打开文件?
    • Galen,我发现了另一个与此相关的问题,您对此的回答似乎对其他用户有效:stackoverflow.com/questions/4057665/…。我可以在我的中使用相同的代码吗?如果是,如何显示计算出的距离?
    • 这行不通,因为您的地图上没有标记,您有一个 kml 图层。从 kml 层中提取标记超出了这些 cmets 的范围。
    猜你喜欢
    • 2014-03-05
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多