Как получить все координаты с карты? - PullRequest
0 голосов
/ 22 мая 2011

Вот мой проект API Google Maps:

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

  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>

Как мне, после добавления нескольких маркеров, получить их и сохранить в xml?

спасибо

Ответы [ 2 ]

0 голосов
/ 23 мая 2011

То, что вы, вероятно, после (если вы выводите материал в XML), это только координаты каждого добавленного маркера, а не весь объект маркера Google, приведенный ниже, это пример того, как получить координаты и сериализовать их (плохо) в 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 голосов
/ 23 мая 2011

Я почти уверен, что это не сработает. Маркеры знают, к какой карте они принадлежат, но не наоборот. Обычно вы делаете это, имея массив, и каждый раз, когда вы добавляете маркер на карту, вы также добавляете его в свой массив. Всякий раз, когда вы хотите манипулировать вашими маркерами, вы затем запускаете этот массив и делаете то, что нужно сделать:)

...