- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在尝试将 Google map 自动完成的结果偏向放置在 map 上的最后一个图钉时遇到问题。我在这里关注谷歌的教程: https://developers.google.com/maps/documentation/javascript/places-autocomplete#change_search_area
我的 CoffeeScript 代码如下:
initSearch = ->
autocomplete = new (google.maps.places.Autocomplete)(document.getElementById('location-query'), types: [ 'geocode' ])
autocomplete.addListener 'place_changed', ->
place = autocomplete.getPlace()
addDestination(place, map, insertIndex) #places pin on map
console.log 'autocomplete bounds (before): ' + autocomplete.getBounds()
#setting bounds around new place
biasCircle = new google.maps.Circle
center: place.geometry.location
radius: 500 #in kilometers
autocomplete.setBounds(biasCircle.getBounds())
console.log 'autocomplete bounds (after): ' + autocomplete.getBounds()
编译成以下 javascript:
initSearch = function() {
var autocomplete;
autocomplete = new google.maps.places.Autocomplete(document.getElementById('location-query'), {
types: ['geocode']
});
return autocomplete.addListener('place_changed', function() {
var biasCircle;
place = autocomplete.getPlace();
addDestination(place, map, insertIndex);
console.log('autocomplete bounds (before): ' + autocomplete.getBounds());
biasCircle = new google.maps.Circle({
center: place.geometry.location,
radius: 500
});
autocomplete.setBounds(biasCircle.getBounds());
return console.log('autocomplete bounds (after): ' + autocomplete.getBounds());
});
};
图钉正确放置在 map 上,但无论我添加多少个地方,console.logs 总是返回:
autocomplete bounds (before): undefined
autocomplete bounds (after): ((9.923577823579402, -84.09528446042509), (9.932560976420598, -84.08616473957488))
边界设置正确(如第二个 console.log
所示),但结果实际上并没有偏差,第一个总是导致未定义。
我们用来测试结果是否有偏差的测试用例是: 1. 在自动完成中输入“San Jose”(最佳结果应该在美国) 2. 在 map 上添加“Limon, Costa Rica”作为标记 3. 在自动完成中输入“San Jose”,最上面的结果应该是 San Jose, COSTA RICA
我们认为这可能是范围问题,所以我们在所有地方都尝试了 window.autocomplete
,但仍然遇到同样的问题。我们还尝试在单独的方法中设置自动完成范围(而不是 place_changed 事件,但这也没有用)。
预先感谢您的帮助!
更新:尝试 Cedric 的回答根据 Cedric 的建议,我试图在监听器回调之外设置边界(从监听器内部调用方法),但我仍然遇到同样的问题。新代码:
#Sets autocomplete bias
setAutocompleteBias = (place) ->
console.log 'autocomplete bounds (before): ' + window.autocomplete.getBounds()
#setting bounds around new place
biasCircle = new google.maps.Circle
center: place.geometry.location
radius: 500 #in kilometers
window.autocomplete.setBounds(biasCircle.getBounds())
console.log 'autocomplete bounds (after): ' + window.autocomplete.getBounds()
window.autocomplete = null
#Autocomplete search box initialization
initSearch = ->
window.autocomplete = new (google.maps.places.Autocomplete)(document.getElementById('location-query'), types: [ 'geocode' ])
#autocomplete.bindTo('bounds', map)
window.autocomplete.addListener 'place_changed', ->
place = window.autocomplete.getPlace()
addDestination(place, map, insertIndex)
setAutocompleteBias(place)
最佳答案
看来您的自动完成功能缺少必要的 map 边界初始化
initSearch = ->
autocomplete = new (google.maps.places.Autocomplete(
document.getElementById('location-query'), types: [ 'geocode' ])
autocomplete.bindTo('bounds', map)
autocomplete.addListener 'place_changed', ->
place = autocomplete.getPlace()
addDestination(place, map, insertIndex) #places pin on map
console.log 'autocomplete bounds (before): ' + autocomplete.getBounds()
#setting bounds around new place
biasCircle = new google.maps.Circle
center: place.geometry.location
radius: 500 #in kilometers
autocomplete.setBounds(biasCircle.getBounds())
console.log 'autocomplete bounds (after): ' + autocomplete.getBounds()
或通过特定位置
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));
var input = document.getElementById('searchTextField');
var options = {
bounds: defaultBounds,
types: ['establishment']
};
autocomplete = new google.maps.places.Autocomplete(input, options);
教程中的链接 https://developers.google.com/maps/documentation/javascript/places-autocomplete#add_autocomplete
来自文档的链接 https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete
关于javascript - GoogleMaps v3 autocomplete.getBounds() 设置后返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33841792/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!