【发布时间】:2016-03-10 19:41:08
【问题描述】:
我正在使用谷歌地图 API 在地图上创建标记。 我正在创建一个基于 lat/lng 的常规标记。如果缺少,我将依赖物理地址并对其进行地理编码。 如果我有超过 50 个地址,我在地理编码时会遇到 OVER_QUERY_LIMIT。我读过一些建议使用计时器的帖子。 在这种情况下,我应该在哪里引入计时器或延迟?
编辑:添加了一个计时器,但效果不佳。浏览器会在一段时间后弹出。
这是我的代码:
$scope.loadMap = function(){
$("#map").width(942).height(400);
google.maps.event.trigger(map, "resize");
var storeLatLng = new google.maps.LatLng(40.0000, -98.0000);
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(40.0000, -98.0000),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var bounds = new google.maps.LatLngBounds();
if(!map){
map = new google.maps.Map($('#map')[0], mapOptions);
}
else{
map.setOptions(mapOptions);
}
for (i = 0; i < $scope.addresses.length; i++) {
var storeItem = $scope.addresses[i];
if(storeItem.latitudeOrg && storeItem.longitudeOrg){
$log.info("latlng for store:" + storeItem.city + ":" + storeItem.latitudeOrg +":"+storeItem.longitudeOrg);
storeLatLng = new google.maps.LatLng(storeItem.latitudeOrg, storeItem.longitudeOrg);
var marker1 = new google.maps.Marker({
position: storeLatLng,
map: map,
title: storeItem.city
});
}
else{ // Rely on physical address (city, state) if lat/lng is either empty or null
var address = storeItem.address1 + "," + storeItem.city + "," + storeItem.state;
$log.info("address for store:" +storeItem.id+":"+ ":" + address);
var city = storeItem.city;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address' : address
}, function(results, status) {
if ( status == google.maps.GeocoderStatus.OK ) {
var locality='';
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
for(var i=0; i<results[0].address_components.length; i++){
var component = results[0].address_components[i];
if(component.types[0] == 'locality'){
locality = component.long_name;
}
}
storeLatLng = new google.maps.LatLng(lat, lng);
var marker2 = new google.maps.Marker({
position: storeLatLng,
map: map,
title: locality.toUpperCase()
});
} else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
$log.info("OVER_QUERY_LIMIT");
setTimeout(function() {
$scope.loadMarkers();
}, 100);
} else {
$log.info("Geocode was not successful for the following reason:" + status);
}
});
}
}
}
【问题讨论】:
-
对它们进行一次地理编码并存储结果
-
查看使用条款“Google Maps Geocoding API 只能与 Google 地图一起使用;禁止在未在地图上显示的情况下对结果进行地理编码。”
-
无缓存或存储。您不会预取、缓存、索引或存储任何要在服务之外使用的内容,但由于网络延迟(以及不是为了阻止 Google 准确跟踪使用情况),并且仅在此类存储:
-
@William - 我不确定你在说什么?我正在地图上显示地理编码地址。
标签: javascript angularjs google-maps geocode