Google maps api 3 避免在Google地图中使用geocoder.geocode,并使用变量传递到Google.maps.Marker
我在我的一个项目中使用了GoogleMapsAPI版本3,它工作正常。 我使用此函数设置标记以指向客户地址Google maps api 3 避免在Google地图中使用geocoder.geocode,并使用变量传递到Google.maps.Marker,google-maps-api-3,google-maps-markers,Google Maps Api 3,Google Maps Markers,我在我的一个项目中使用了GoogleMapsAPI版本3,它工作正常。 我使用此函数设置标记以指向客户地址 function codeAddress() { var address = 'ulitsa "Dimcho Debelyanov" 3, Sofia, Bulgaria'; geocoder.geocode({ 'address': address}, function (results, status) { if (status == google.ma
function codeAddress() {
var address = 'ulitsa "Dimcho Debelyanov" 3, Sofia, Bulgaria';
geocoder.geocode({ 'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
map.setZoom(16);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
但有时对地理编码器的请求并没有成功,也并没有构建标记。
我已尝试将结果数组硬编码为变量(result from results.toSource())
并直接传递到
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
但这不能正常工作,因为.toSource只保留变量。我收到以下错误:
InvalidValueError:setCenter:不是LatLng或LATLNERAL:在属性lat:不是数字中
我认为google.maps.Marker需要对象作为参数。
当我将对象展示给调试器时,我看到它从prototype链收到了几个方法:
__proto__: Q
b: function (a){return a?Md(this.lat(),a.lat())&&Md(this.lng(),a.lng()):!1}
constructor: function Q(a,b,c){a-=0;b-=0;c||(a=Kd(a,-90,90),180!=b&&(b=Ld(b,-180,180)));this.nb=a;this.ob=b}
equals: function (a){return a?Md(this.lat(),a.lat())&&Md(this.lng(),a.lng()):!1}
lat: function (){return this[a]}
lng: function (){return this[a]}
toString: function (){return"("+this.lat()+", "+this.lng()+")"}
toUrlValue: function (a){a=Sd(a)?a:6;return We(this.lat(),a)+","+We(this.lng(),a)}
__proto__: Object
所以,如果我将lat:function(){returnthis[a]}
放入对象中,这是不够的。
这个问题很有趣。我正在寻找解决这个问题的办法
如果有任何想法和帮助,我将不胜感激
在霍利斯特给出了很好的回答之后,我已经将我的代码重写为
函数完全避免地理编码器(我从地理编码器获得纬度和长度
答复)
这可能会帮助有类似问题的人 我不太清楚你想做什么。如果您已经拥有lat&lng位置,则不需要地理编码器。如果地理编码器可用,则没有问题 如果在地理编码器不可用的情况下设置标记,可能这是一个解决方案 其
position
属性所需的标记
都是LatLng
对象(这就是结果[0]。几何体。位置
对象)。您可以在一次成功的地理代码调用中缓存此by地址,然后从该地址查找:
var cachedAddresses = {};
function cacheAddress(address, location) {
cachedAddresses[address] = new google.maps.LatLng(location.lat(), location.lng());
};
function createMarker(location, address) {
map.setCenter(location);
var marker = new google.maps.Marker({
map: map,
position: location,
title: address || location.toString()
});
map.setZoom(16);
};
if (cachedAddresses[address]) {
console.log('getting address from cache');
createMarker(cachedAddresses[address], address);
} else {
console.log('geocoding address...');
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// cache the address for future lookup
cacheAddress(address, results[0].geometry.location);
createMarker(results[0].geometry.location, address);
} else {
console.log('Geocode failed: ' + status);
}
});
}
这也可以通过按地址存储lat和lng值并为标记调用重新配置LatLng对象来持久化。类似这样的JSON结构可能会起作用:
{address: 'the address':
{
lat: 42.6721804,
lng: 23.3543842
}
}
不过,这将非常具体。地址的细微差异将丢失缓存
这里有一个例子来说明这一点。非常感谢。正是我需要的“标记的position属性所需要的是一个LatLng对象(这就是结果[0].Geometry.location对象)。”
{address: 'the address':
{
lat: 42.6721804,
lng: 23.3543842
}
}