2011-05-22 2 views
0

Voici mon projet api google maps:Comment obtenir tous les coords d'une carte?

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

comment puis-je, après l'ajout de plusieurs marqueurs, les obtenir et enregistrer dans xml?

grâce

Répondre

0

Je suis assez sûr que cela ne fonctionne pas de cette façon. Les marqueurs savent à quelle carte ils appartiennent, mais pas l'inverse. La façon dont vous le feriez normalement serait d'avoir un tableau, et chaque fois que vous ajoutez un marqueur à votre carte, vous l'ajoutez également à votre tableau. Chaque fois que vous voulez manipuler vos marqueurs, vous devez parcourir ce tableau et faire ce qui doit être fait :)

0

Ce que vous êtes probablement après (si vous produisez du contenu en XML) est juste les coordonnées de chaque marqueur ajouté plutôt que le Tout l'objet marqueur Google ci-dessous est un exemple de comment obtenir les coordonnées et sérialiser (mal) en 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> 
Questions connexes