Javascript地理位置 [英] Javascript Geolocation

查看:74
本文介绍了Javascript地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对地理位置进行一些处理.我想以一个变量作为数组来获取地理位置的纬度和经度.它不起作用.我认为代码中的第一行就是问题所在.是否有可能获得像这样的值(带有返回值)?

I'm trying to do some things with geolocation. I want to get back the geolocation latitude and longitude in one variable as array. It doesn't work. I think the first line in the code is the problem. Is it possible to get the value like that (with return)?

startingPoint = navigator.geolocation.getCurrentPosition(onGeoSuccess);

function onGeoSuccess(position) {
    start['lat'] = position.coords.latitude;
    start['lng'] = position.coords.longitude;
    return start;
}

有更好的解决方案吗?我不想在onGeoSuccess中执行其他代码,我想将值返回.

Is there a better solution? I don't want to execute other code in the onGeoSuccess, I want to return the value back.

推荐答案

您可以尝试执行以下操作:

You can try something like this:

function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
        position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

这篇关于Javascript地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆