How to show location on a map taking longitude and latitude values from XML [duplicate]

戏子无情 提交于 2019-12-12 00:43:29

问题


This is an XML entry and it takes geolocation from PHP form. I want to display those values as a map in 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>

回答1:


how are you generating this XML? I am assuming you use php to generate it. This here will do it via JavaScript, assuming you're using google-maps API.

maybe this can get you started:

 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,
     }); 

to create the info window:

 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

      });
    }

hopefully some of this can help




回答2:


You want to show these LatLong points as markers, I assume. Here is how to do that in google maps.

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);

See more at https://developers.google.com/maps/documentation/javascript/markers

Similarly you can do it in mapbox like this

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);


来源:https://stackoverflow.com/questions/29222887/how-to-show-location-on-a-map-taking-longitude-and-latitude-values-from-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!