【发布时间】:2013-11-07 12:51:28
【问题描述】:
我必须从地址数组中实现多个标记功能。正在从数据库中获取地址字符串。
我的地址数组如下所示
var address = <?php echo $add_js ?>;
我在互联网上甚至在这个论坛上浏览了很多示例,但在大多数示例中,纬度和经度已经在这些数据库中可用。有什么办法可以让我使用该地址数组并在谷歌地图上放置多个标记。 或解释此类概念的任何示例?!
我已经从 JSFIDDLE 练习了这个例子,但我没有得到任何输出。
<script>
var geocoder;
var map;
var markersArray = [];
function initialize()
{
geocoder = new google.maps.Geocoder();
latlang = geocoder.geocode( {
'address': 'New Delhi, India'},
function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
markersArray.push(marker);
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
var myOptions =
{
center: latlang, zoom: 5,
mapTypeId: google.maps.MapTypeId.SATELLITE,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.SMALL
}
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
plotMarkers();
}
var locationsArray = new Array(
"New Delhi, India", "Gurgaon,Haryana, India", "Mumbai, India",
"Noida Sector-63,India","Banglore, Karnataka,India");
function plotMarkers(){
for(var i = 0; i < locationsArray.length; i++){
codeAddresses(locationsArray[i]);
}
}
function codeAddresses(address){
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
//markersArray.push(marker);
}
else{
alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
【问题讨论】:
-
如果您的数组中有超过 10 个地址,您将遇到地理编码器的配额和查询限制。您不想在页面加载时对地址进行地理编码,当然也不希望知道地址(除非您构建自己的地理编码器)。
-
@geocodezip 那么我应该遵循哪些步骤才能使其可行?我当然有超过 10 个地址的数据库。
-
你应该离线地理编码这些地址(如果你使用谷歌的地理编码器,你只能暂时“缓存”它们,所以你需要添加一个机制来定期重新地理编码地址),然后使用坐标来在页面加载时放置标记。
-
@geocodezip 先生,您能给我一些关于这个概念的教程链接吗?
-
嘿,agnes 如果您有超过 10 个地址会发生什么,您找到解决方案了吗?
标签: javascript arrays google-maps google-maps-api-3