首先,我想向您展示的解决方案我已经尽力了,所以你们没有使用这些之一:谷歌API的地方在地图上放置多个标记,并放大到最标记是
当我通过地方下面的循环:
service.textSearch({query:query}, function(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
//for each result in results.length ++
for (var i = 0; i < results.length; i++) {
//here I'm just setting the names from the variables into a list, to display the names as I show in (Figure 1).
var item = document.createElement('li');
item.appendChild(document.createTextNode(results[i].name));
document.getElementById('results').appendChild(item);
//here I set my variables that are necessary for the markers to work
p_id[i] = results[i].place_id;
lat[i] = results[i].geometry.location.lat();
lng[i] = results[i].geometry.location.lng();
//here I initialize the map with the given values.
initMap(p_id, lat, lng, i);
}
}
});
由于这个循环完成它去的地方放置地图上的标记。
注:我不想摆脱它再次创造,因为这对我来说有非常有用的places
的,再加上它似乎并不妨碍我做的尝试之作。
至于砂矿地图本身上的标记:
function initMap(p_id, lat, lng, i) {
//this creates the position of the map
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: lat[i], lng: lng[i]},
zoom: 13
});
var infowindow = new google.maps.InfoWindow(), marker, i;
var service = new google.maps.places.PlacesService(map);
var marker;
//gets the details of the placeid over again
service.getDetails({
placeId: p_id[i]
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
//this is where the marker is created with position and animation
marker = new google.maps.Marker({
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng(lat[i], lng[i]),
map: map
});
//this is the info if you click the marker
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + 'Place ID: ' + place.place_id + '<br>' + place.formatted_address + '</div>');
infowindow.open(map, marker);
}
})(marker, i));
}
});
}
如果只有1位,则1个标记显示出来,但是当他们有更多的地方,则有完全没有标记很奇怪。如..我显示图2
如果有人有任何线索可以解决它,请告诉他们。我似乎无法自拔,尽管我一直在寻找一段时间。我试图做一个的jsfiddle,但可悲的API不能够在jsfddle运行...
编辑:
的多个标记问题是由SeanKendle解决。问题是像我已经怀疑,我不停地创建多个地图...
我干脆搬到了maps
了我mapinit
功能,并在getPlaces
功能把它放在我的service
上面是这样的:
var map = new google.maps.Map(document.getElementById('map'), {
center: latlng,
zoom: 5
});
service = new google.maps.places.PlacesService(
document.getElementById('attributions') //attributions-container
);
//send a query
service.textSearch({query:query}, function(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var item = document.createElement('li');
item.appendChild(document.createTextNode(results[i].name));
document.getElementById('results').appendChild(item);
p_id[i] = results[i].place_id;
lat[i] = results[i].geometry.location.lat();
lng[i] = results[i].geometry.location.lng();
initMap(p_id, lat, lng, i, map);
}
}
});
我现在面临的最后一个问题是我需要放大标记数最多的地方。现在我只是简单地设置经纬度,因为这应该开始:
var latlng = new google.maps.LatLng(20.540221, -4.042969);
在此先感谢!
你在'for'循环一遍又一遍初始化地图?这是否真的初始化地图? 'initMap(p_id,lat,lng,i);' – SeanKendle
不幸的是,我已经在想这会是问题,但不知道如何解决这个问题 –
将它移出循环? – SeanKendle