Javascript 谷歌地图地理编码地址与自定义标签?
在过去的一天左右,我一直把头撞在桌子上,弄不明白这是怎么回事。每当将标记添加到我的地图时,它们都会随机生成其标签。这很好,但是页面上调用了PHP代码,以列出的顺序显示位置。此列表必须与标记匹配。我想使用位置数组中的数字作为我的标签,但无论我如何尝试,我都无法使label:locations[I][2](或任何变体)起作用 我尝试添加一个变量,该变量等于for循环中的位置[I][2],但它只存储并显示在marker函数中调用该变量时的最后一个数字 希望这是有意义的Javascript 谷歌地图地理编码地址与自定义标签?,javascript,php,google-maps,google-maps-api-3,Javascript,Php,Google Maps,Google Maps Api 3,在过去的一天左右,我一直把头撞在桌子上,弄不明白这是怎么回事。每当将标记添加到我的地图时,它们都会随机生成其标签。这很好,但是页面上调用了PHP代码,以列出的顺序显示位置。此列表必须与标记匹配。我想使用位置数组中的数字作为我的标签,但无论我如何尝试,我都无法使label:locations[I][2](或任何变体)起作用 我尝试添加一个变量,该变量等于for循环中的位置[I][2],但它只存储并显示在marker函数中调用该变量时的最后一个数字 希望这是有意义的 var locations
var locations = [
['text','address1','1'],
['text','address2','2'],
];
var labels = '123456789';
var labelIndex = 0;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(38.3509665,-81.6881185),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
var marker, i;
for (i = 0; i < locations.length; i++) {
geocodeAddress(locations[i]);
}
function geocodeAddress(location) {
geocoder.geocode( { 'address': location[1]}, function(results, status) {
//alert(status);
if (status == google.maps.GeocoderStatus.OK) {
//alert(results[0].geometry.location);
map.setCenter(results[0].geometry.location);
createMarker(results[0].geometry.location,location[0]+"<br>"+location[1]);
}
else
{
alert("some problem in geocode" + status);
}
});
}
function createMarker(latlng,html){
var marker = new google.maps.Marker({
position: latlng,
map: map,
label: labels[labelIndex++ % labels.length],
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
google.maps.event.addListener(marker, 'mouseout', function() {
infowindow.close();
});
}
google.maps.event.addDomListener(window, 'load', initialize);
var位置=[
['text'、'address1'、'1'],
['text'、'address2'、'2'],
];
变量标签='123456789';
var labelIndex=0;
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新google.maps.LatLng(38.3509665,-81.6881185),
mapTypeId:google.maps.mapTypeId.ROADMAP,
disableDefaultUI:true
});
var trafficLayer=new google.maps.trafficLayer();
trafficLayer.setMap(地图);
var infowindow=new google.maps.infowindow();
var geocoder=new google.maps.geocoder();
var标记,i;
对于(i=0;i”+位置[1]);
}
其他的
{
警报(“地理代码中的某些问题”+状态);
}
});
}
函数createMarker(latlng,html){
var marker=new google.maps.marker({
位置:latlng,
地图:地图,
标签:标签[labelIndex++%labels.length],
});
google.maps.event.addListener(标记'mouseover',function(){
setContent(html);
信息窗口。打开(地图、标记);
});
google.maps.event.addListener(标记'mouseout',function(){
infowindow.close();
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
提前谢谢 这里是更新后的函数,我为
createMarker
添加了第三个变量,名为label
,我将location[2]
传递给这个变量
function geocodeAddress(location) {
geocoder.geocode( { 'address': location[1]}, function(results, status) {
//alert(status);
if (status == google.maps.GeocoderStatus.OK) {
//alert(results[0].geometry.location);
map.setCenter(results[0].geometry.location);
createMarker(results[0].geometry.location,location[0]+"<br>"+location[1],location[2]);
}
else
{
alert("some problem in geocode" + status);
}
});
}
function createMarker(latlng,html,label){
var marker = new google.maps.Marker({
position: latlng,
map: map,
label: label
});
功能地理编码地址(位置){
geocoder.geocode({'address':位置[1]},函数(结果,状态){
//警报(状态);
if(status==google.maps.GeocoderStatus.OK){
//警报(结果[0]。几何体。位置);
map.setCenter(结果[0].geometry.location);
createMarker(结果[0]。几何体。位置,位置[0]+“
”+位置[1],位置[2]);
}
其他的
{
警报(“地理代码中的某些问题”+状态);
}
});
}
函数createMarker(latlng、html、label){
var marker=new google.maps.marker({
位置:latlng,
地图:地图,
标签:标签
});
我的天啊。我不敢相信这会那么容易。我现在觉得自己很笨。谢谢!!!