Google地图API多个标记 [英] Google maps API multiple markers

查看:154
本文介绍了Google地图API多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码显示Google地图,并在地图上创建标记。它的工作很好。我只需要在同一张地图上放置多个标记。

i am using following code to show google map and also create a marker on the map. Its working well. I just need to put multiple markers on the same map.

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js?    
key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>

<script>
var myCenter=new google.maps.LatLng(51.508742,-0.120850);

function initialize()
{
var mapProp = {
center:myCenter,
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var marker=new google.maps.Marker({
position:myCenter,
});

marker.setMap(map);

var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});

infowindow.open(map,marker);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body> 
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

任何人都可以帮助我使用多个地址在同一个地图上放置多个标记。
谢谢

Can anyone help me to drop multiple markers on the same map by using multiple addresses. Thanks

推荐答案

这是一个基本问题,可以在网上找到几个例子。你基本上需要一个数组来保存各种标记lat lng's。然后使用循环将这些latlng放置在地图上,就像使用一个标记一样。

This is a basic question and several examples can be found in the web. You basically need an array to hold the various markers lat lng's. Then use a loop to place these latlng's on the map just like you do with one marker.

var berlin = new google.maps.LatLng(52.520816, 13.410186);
var neighborhoods = [
new google.maps.LatLng(52.511467, 13.447179),
new google.maps.LatLng(52.549061, 13.422975),
new google.maps.LatLng(52.497622, 13.396110),
new google.maps.LatLng(52.517683, 13.394393)
];
var markers = [];
var map;

function initialize() {
var mapOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: berlin
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
drop();
}

function drop() {
 for (var i = 0; i < neighborhoods.length; i++) 
  {
    markers.push(new google.maps.Marker({
    position: neighborhoods[i],
    map: map,
    }));
  }
}

这篇关于Google地图API多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆