[英]No geolocation on google maps, how to ask for persons location
我目前正在使用谷歌地图,并使用地理位置绘制人员位置。 我已经使用了来自Google示例的代码,该代码允许进行nogeolocation响应,它基本上要求提供一组硬编码的坐标。 我想做的是有一个带有模式选择框的js模态弹出窗口,其中包含城市列表。 选择其中一个城市后,地图便会填充到该城市。 我有一个城市列表,与它们关联的值存在坐标。 我相信我目前的代码存在的问题是它无法通过下拉框设置之前跳过变量。
.js文件
function handleNoGeolocation(errorFlag) {
if(errorFlag == true) {
//alert("Geolocation service failed.");
$('#myModal').modal('show');
$('#citiesList').change(function(){
var location = $(this).val();
parseFloat(location);
});
initialLocation = new google.maps.LatLng(location);
} else {
alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
initialLocation = siberia;
}
map.setCenter(initialLocation);
}
html的
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<select id="citiesList" class="form-control">
<option value=''>Select City...</option>
<option value="53.5412083, -113.29573650">Sherwood Park</option>
<option value="51.04532,-114.058106">Calgary</option>
</select>
</div>
</div>
</div>
</div>
您正在更改事件函数中创建变量,然后尝试在不可见的变量外部使用它。 当Javascript处于同步状态时,在这里不会触发change事件,而只是在设置它,因此,当change事件触发时, handleNoGeolocation
的代码已经执行。
$('#myModal').modal('show');
$('#citiesList').change(function(){
//creates a local scope variable 'location' visible only in this function
var location = $(this).val();
//note this isnt being used as you are not setting the result to anything
parseFloat(location);
});
//'location' here is undefined
initialLocation = new google.maps.LatLng(location);
在change函数中可以看到变量的地方进行调用
编辑:
$('#myModal').modal('show');
$('#citiesList').change(function(){
var location = $(this).val();
//splits the string on ',', so we can use the lat lng separately
location = location.split(",");
//use $.trim to remove any of the whitespaces
initialLocation = new google.maps.LatLng($.trim(location[0]),$.trim(location[1]));
map.setCenter(initialLocation);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.