谷歌地图与地理编码器的问题
重点是指出 JSON 文件中的标记,但如果当前的 json 没有任何 lat 和 lng,它应该计算它,但它不起作用,
//For example when
item.county = 'Orust'
//and
item.region = 'Bohuslän'
为什么会这样?
if(lat == null)
{
geocoder.geocode({ 'address': item.county+', '+item.region}, function(result, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(result[0])
{
latlng = result[0].geometry.location
}
}
});
我错过了什么吗?
The point is to point out the markers from a JSON file, but if the current json, dont have any lat and lng it should calculate it, but it dont work
//For example when
item.county = 'Orust'
//and
item.region = 'Bohuslän'
Why is that?
if(lat == null)
{
geocoder.geocode({ 'address': item.county+', '+item.region}, function(result, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(result[0])
{
latlng = result[0].geometry.location
}
}
});
Have I missed something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先检查回调内的情况是否按预期工作。为此,请将以下内容更改
为:(
您也可以将
console.log
添加到latlng
赋值上方,而不是更改它。实际上,这可能更好。)如果 JavaScript 控制台中生成的消息看起来像一个
latlng
对象,则问题可能是范围问题 - 换句话说,回调之外的latlng
不是与里面的latlng
相同回调。如果 JavaScript 控制台中生成的消息看起来不像
latlng
对象,则问题出在回调本身内部。通过将其放在上面最后一个console.log()
的位置来检查是否获得任何结果:如果这些
console.log
命令从未触发且没有任何结果出现在您的 JavaScript 控制台中,则从geocoder.geocoder()
返回的状态不是OK
,您需要在回调中添加一些错误处理以查看发生了什么。First check to see if things are working as expected inside the callback. To do that, change this:
To this:
(You can also just add the
console.log
above thelatlng
assignment rather than changing it. Actually, that's probably better.)If the resulting message in your JavaScript console looks like a
latlng
object, then the problem is likely a scope problem--in other words,latlng
outside of the callback is not the same aslatlng
inside the callback.If the resulting message in your JavaScript console does not look like a
latlng
object, then the problem is inside the callback itself. Check to see if you are getting any results at all by putting this where you put the lastconsole.log()
above:If these
console.log
commands never fire and nothing appears in your JavaScript console, then the status returned from thegeocoder.geocoder()
is notOK
and you need to add some error handling in the callback to see what's going on.对地理编码器的请求异步运行。您无法从外部代码访问回调内部分配的变量。实际上你可以,但它是未定义的。
Request to geocoder runs asynchronously. You cannot access to vriable which is assigned inside the callback from the outside code. Actually you could but it will be undefined.