【问题标题】:How to show location on a map taking longitude and latitude values from XML [duplicate]如何在地图上显示从 XML 获取经度和纬度值的位置 [重复]
【发布时间】:2015-05-27 04:32:12
【问题描述】:

这是一个 XML 条目,它采用 PHP 形式的地理定位。我想将这些值显示为 HTML 中的地图:

 <entries>
   <entry>
   <name>Anny</name>
   <email>anny1@hotmail.com</email>
   <place>Fridays</place>
   <comment>Very Good</comment>
   <food>Jack Daniels Burger</food>
   <kitchen>American</kitchen>
   <rating>5</rating>
   <latitude>34.7618259</latitude>
   <longitude>33.0283905</longitude>
   <picture/>
   </entry>
</entries>

【问题讨论】:

    标签: javascript html google-maps xml-parsing geolocation


    【解决方案1】:

    你是如何生成这个 XML 的?我假设你使用 php 来生成它。 假设您使用的是 google-maps API,这将通过 JavaScript 完成。

    也许这可以让你开始:

     var xml = "your xml file"          
         markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = entries[i].getAttribute("name"); 
          var email = entries[i].getAttribute("email");
          var place ..... an so on
      var lat = entries[i].getAttribute("latitude");
      var lng = entries[i].getAttribute("longitude");
    
      //the creation of point
      point = new google.maps.LatLng(
              lat,
              lng);
        //create actual marker 
      marker = new google.maps.Marker({
        map: map,
        position: point,
         }); 
    

    创建信息窗口:

     infoWindow = new google.maps.InfoWindow;
    var html =  name + "&nbsp;" + email + '&nbsp;' place ; //+ ... whatever else 
    
    bindInfoWindow(marker, map, infoWindow, html);
    
    function bindInfoWindow(marker, map, infoWindow, html) {
          google.maps.event.addListener(marker, 'click', function() {
              infoWindow.close();
            infoWindow.setContent(html);
            infoWindow.open(map, marker, html);
            map.setCenter(marker.getPosition()); // this will center the map on the clicked marker
    
          });
        }
    

    希望这能有所帮助

    【讨论】:

      【解决方案2】:

      我假设您想将这些 LatLong 点显示为标记。这是在谷歌地图中执行此操作的方法。

      var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      
      var marker = new google.maps.Marker({
       position: myLatlng,
       title:"Hello World!"
      });
      
      // To add the marker to the map, call setMap();
      marker.setMap(map);
      

      https://developers.google.com/maps/documentation/javascript/markers查看更多信息

      类似地,你可以像这样在 mapbox 中做到这一点

      L.mapbox.featureLayer({
      // this feature is in the GeoJSON format: see geojson.org
      // for the full specification
      type: 'Feature',
      geometry: {
          type: 'Point',
          // coordinates here are in longitude, latitude order because
          // x, y is the standard for GeoJSON and many formats
          coordinates: [
            -77.03221142292,
            38.913371603574 
          ]
      }).addTo(map);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 2013-02-26
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多