Google 地图使用自定义标签对地址进行地理编码
Google Maps geocode addressess with custom labels?
过去一天左右我一直在用头撞桌子,但无法弄清楚这一点。每当将标记添加到我的地图时,它们都会随机生成标注。这很好,但是页面上调用了按列出的顺序显示位置的PHP代码。此列表必须与标记匹配。我想使用位置数组中的数字作为我的标签,但无论我尝试什么,我都无法让标签:位置[i][2](或任何变体)工作。
我尝试在 for 循环中添加一个等于位置 [i][2] 的变量,但它只是在标记函数中调用变量时存储并显示最后一个数字。
希望这是有道理的。
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);
提前谢谢。
这是更新的函数,我为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
});
相关文章:
- 如何从地理编码地址查找纬度液化天然气
- 如何使用谷歌地理编码将地址转换为坐标后控制台日志数据
- 为什么我的地理编码无法显示地址
- 是否有JavaScript或可能的Python/Django web服务来对街道地址进行地理编码
- 将地址地理编码为 GPS 坐标
- 使用地址进行地理编码 - over_query_limit问题
- 在ST_INTERSECTS中使用“地理编码地址”
- Google 地图使用自定义标签对地址进行地理编码
- 谷歌地图 api v3 对多个地址进行地理编码
- 谷歌地理编码器返回错误的地址坐标
- 列出使用 Google 地图地理编码器 API 获取的一组地址
- 地理编码器 API 仅返回街道地址
- 从反向地理编码回调中获取地址
- 与谷歌地图网站不同,谷歌地图地理编码器找不到不是确切地址的地点
- 谷歌地图反向地理编码地址出现在第二次点击后
- 反向地理编码地址的准确性
- 谷歌地理编码地址细分
- 如何在信息窗口中显示我的地理编码地址?谷歌地图API
- 通过Google maps api编码地址并延迟结果
- 在信息窗口中显示反向地理编码地址