Javascript 谷歌地图,多个物理地址,多个指针图标,始终显示相同的图标
我需要谷歌地图的帮助。我想创建一个可以使用多个指针显示多个地址的映射。问题是所有地址都会显示最后一个地址的指针图标。这可能与AsynchronousGeocoder.geocode有关,但我是JS初学者,无法解决这个问题 有人能帮忙吗?谢谢Javascript 谷歌地图,多个物理地址,多个指针图标,始终显示相同的图标,javascript,google-maps,asynchronous,Javascript,Google Maps,Asynchronous,我需要谷歌地图的帮助。我想创建一个可以使用多个指针显示多个地址的映射。问题是所有地址都会显示最后一个地址的指针图标。这可能与AsynchronousGeocoder.geocode有关,但我是JS初学者,无法解决这个问题 有人能帮忙吗?谢谢 <html> <head> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></s
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 500px;"></div>
<script type="text/javascript">
var locations = [
['Place A', '2430 N 1060 E, North Logan, UT', 'A'],
['Place B', '495 W Jackson Street, Knoxville, IA', 'B'],
['Place C', '1900 N Flagler Drive, West Palm Beach, FL', 'C']
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 0,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
var image = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + locations[i][2] + '|FF0000|000000',
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34)
);
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {
'address': locations[i][1]
}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
icon: image
});
}
});
}
</script>
</body>
变量位置=[
['Place A','2430 N 1060 E,北洛根,UT','A'],
['Place B','495 W Jackson Street,Knoxville,IA','B'],
佛罗里达州西棕榈滩Flagler大道北侧1900号,C区
];
var map=new google.maps.map(document.getElementById('map'){
缩放:0,
中心:新谷歌地图LatLng(-33.92151.25),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
请尝试以下代码:
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = createMarker(locations[i]);
}
function createMarker(location) {
var image = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + location[2] + '|FF0000|000000',
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34)
);
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {
'address': location[1]
}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
icon: image
});
}
});
}
var标记,i;
对于(i=0;i
尝试以下代码:
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = createMarker(locations[i]);
}
function createMarker(location) {
var image = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + location[2] + '|FF0000|000000',
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34)
);
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {
'address': location[1]
}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
icon: image
});
}
});
}
var标记,i;
对于(i=0;i
您能详细说明一下吗?OP做错了什么?你的代码为什么能工作?@RASG:是关于保存图标的图像变量。另一个解决方案是将图标创建移到回调函数中。您能详细说明一下吗?OP做错了什么?你的代码为什么能工作?@RASG:是关于保存图标的图像变量。另一个解决方案是将图标创建移动到回调函数。