如何从地图中获取所有坐标?

发布于 2024-11-09 07:23:50 字数 1423 浏览 0 评论 0原文

这是我的谷歌地图 api 项目:

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

谢谢

Here is my google maps api project:

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

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

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-11-16 07:23:50

我很确定它不会那样工作。标记知道它们属于哪张地图,但反之则不然。通常执行此操作的方法是拥有一个数组,每次向地图添加标记时,也会将其添加到数组中。每当您想要操作标记时,您就可以运行该数组并执行需要执行的操作:)

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

杯别 2024-11-16 07:23:50

您可能想要的(如果您将内容输出到 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文