Javascript 谷歌地图上没有地理位置,如何询问人员位置

Javascript 谷歌地图上没有地理位置,如何询问人员位置,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我目前正在使用谷歌地图,并使用地理位置绘制一个人的位置。我使用了google示例中允许nogeolocation响应的代码,它基本上要求一组硬编码的坐标。我试图做的是有一个js模式弹出窗口,其中有一个带有城市列表的选择框。选择其中一个城市后,地图将填充到该城市。我有一个城市的列表,与之相关的值在坐标中。我相信我当前的代码存在的问题是,它在通过下拉框设置变量之前跳过了变量 .js .html &时代; 情态标题 选择城市。。。 舍伍德帕克 卡尔加里 您正在change event函数中创建一个变

我目前正在使用谷歌地图,并使用地理位置绘制一个人的位置。我使用了google示例中允许nogeolocation响应的代码,它基本上要求一组硬编码的坐标。我试图做的是有一个js模式弹出窗口,其中有一个带有城市列表的选择框。选择其中一个城市后,地图将填充到该城市。我有一个城市的列表,与之相关的值在坐标中。我相信我当前的代码存在的问题是,它在通过下拉框设置变量之前跳过了变量

.js

.html


&时代;
情态标题
选择城市。。。
舍伍德帕克
卡尔加里

您正在change event函数中创建一个变量,然后尝试在该变量不可见的外部使用它。虽然Javascript是同步的,但这里没有触发更改事件,只是设置了更改事件,因此当更改事件触发
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函数中进行调用

编辑:


为什么不使用内置的较新浏览器呢?我正在使用它,如果他们拒绝或他们的浏览器不支持它,这是一个退路。这是半途而废,我仍然存在的问题是位置的值是一个字符串,它显示为“43.334,-34.43433”并且应该不带引号出现,以便
新的google.maps.LatLng
可以处理它。@user1881482,请参见编辑,您必须拆分字符串,以便分别使用lat和lng
。拆分
正是我要找的。简单的javascript基础。这很有效。如果拒绝使用“地理定位”选项,则为用户提供使用下拉列表选择位置的选项。非常感谢。我还调用
$('#myModal').modal('hide')在更改功能中的
var location
之前,以在选择城市后隐藏模式。
 <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">&times;</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>
$('#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);
$('#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);
});