我有下面的代码,它在页面上放置4张地图,并根据地址为每个地图添加一个标记。但我需要将标记映射到标记的中心。
<script type="text/javascript">
var geocoder;
var map0,map1,map2,map3;
var markers = [];
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map0 = new google.maps.Map(document.getElementById("map_canvas1"), mapOptions);
addPostCode('@address1',map0);
map1 = new google.maps.Map(document.getElementById("map_canvas2"), mapOptions);
addPostCode('@address2',map1);
map2 = new google.maps.Map(document.getElementById("map_canvas3"), mapOptions);
addPostCode('@address3',map2);
map3 = new google.maps.Map(document.getElementById("map_canvas4"), mapOptions);
addPostCode('@address4',map3);
}
function addPostCode(address,map) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
center: results[0].geometry.location,
name: address
});
markers.push(marker);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
不完全确定将其放在该地图标记的中心位置。
答案 0 :(得分:1)
好吧,我编写了一个可以从这里看到的工作示例: JS fiddle
我改变的一些事情是从标记中删除center: results[0].geometry.location
(因为标记只添加到位置并使用map.setCenter();
控制地图)而且我稍微更改了类的名称,因此请将其修改为喜欢。干杯!