开发者

How do I get all coords from a map?

开发者 https://www.devze.com 2023-03-07 18:56 出处:网络
Here is my google maps api project: <head> <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" /&g开发者_Go百科t;

Here is my google maps api project:

<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /&g开发者_Go百科t;
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var rio = new google.maps.LatLng(-22.894125,-43.199358);
  var map;

  function initialize() {
    var myOptions = {
      zoom: 10,
      center: rio,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    google.maps.event.addListener(marker, 'click', toggleBounce);
    }

    function addMarkerAtCenter() {
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        draggable:true,
        animation: google.maps.Animation.DROP,
        map: map
    });
    }

  function toggleBounce() {

    if (marker.getAnimation() != null) {
      marker.setAnimation(null);
    } else {
      marker.setAnimation(google.maps.Animation.BOUNCE);
    }
  }
</script>
</head>
<body onload="initialize()" style="margin:0px; padding:0px;">
  <center><input type="button" value="Adicionar Marcador" onclick="addMarkerAtCenter()"/></center>
  <center><div id="map_canvas" style="width:100%; height:100%"></div></center>
</body>

how can I, after adding multiple markers, get them and save into xml ?

thanks


I am pretty sure that it doesn't work that way. Markers know what map they belong to, but not the other way around. The way you would normally do this would be to have an Array, and each time you add a marker to your map you also add it to your array. Whenever you want to manipulate your markers you then run through that array and do the stuff that needs doing :)


What you are probably after (if you outputing stuff to XML) is just the coordinates of each marker added rather than the whole google marker object below is an example of how to get the coordinates and serialize them (badly) to XML.

<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var rio = new google.maps.LatLng(-22.894125,-43.199358);
  var map;

//you probably just want to store coordinates of markers
var coords = []

  function initialize() {
    var myOptions = {
      zoom: 10,
      center: rio,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   // google.maps.event.addListener(marker, 'click', toggleBounce);
    }

    function addMarkerAtCenter() {
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        draggable:true,
        animation: google.maps.Animation.DROP,
        map: map
    });

    //get the coordinates of the marker
    pos = marker.getPosition();
    //save the coordinates
    coords.push({lat:pos.lat(), lng:pos.lng()})
    }


  function toggleBounce() {

    if (marker.getAnimation() != null) {
      marker.setAnimation(null);
    } else {
      marker.setAnimation(google.maps.Animation.BOUNCE);
    }
  }
  //a primitive serialize function - add something more sophisticated
function serialize(arr) {
xml = "<markers>";
for (i=0;i<arr.length;i++) {
xml += "<marker>";
for(var prop in arr[i]) {
    if(arr[i].hasOwnProperty(prop))
       xml += "<" + prop +">" + arr[i][prop] + "</" + prop +">";
}
xml += "</marker>";
}


xml +="</markers>";
//do something with the result
alert(xml);
}
</script>
</head>
<body onload="initialize()" style="margin:0px; padding:0px;">
  <center><input type="button" value="Adicionar Marcador" onclick="addMarkerAtCenter()"/>  <input type=button onClick="javascript:serialize(coords)" value="To XML"></center>
  <center><div id="map_canvas" style="width:100%; height:100%"></div></center>

</body>
0

精彩评论

暂无评论...
验证码 换一张
取 消