从控制器

时间:2016-09-30 11:47:24

标签: javascript php google-maps-api-3 navigator

我使用navigator.geolocation.getcurrentposition来获取当前用户的位置(lat / long)。这是我的功能:

function userLocation(){

    var response = [];
    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(function(p){
            // on success
            response['latitude'] = p.coords.latitude;
            response['longitude'] = p.coords.longitude;

            console.log(response);

        }, function(e){
            // on error
            console.log(e);
        });

    } else {
        console.log("Geolocation is not supported by this browser.");
    }

    return response;

}

它总是返回空数组,我理解getCurrentPosition函数进行异步调用,因此无法返回响应。

我尝试使用google geocode api获取位置,但这是基于用户的IP地址而不是那么准确。

我想要一个函数来返回这些lat / long。有什么办法可以在我的控制器函数中得到getCurrentPosition的响应吗?

4 个答案:

答案 0 :(得分:1)

getLocation()功能放置在窗口加载中,并且一个弹出窗口将要求允许您的位置,如果您允许您的经度和经度将被抓取。

请使用以下代码。

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        console.log("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
   console.log("Latitude: " + position.coords.latitude +
    "<br>Longitude: " + position.coords.longitude);
    $.ajax({
      url: 'abc.php',
      type: 'POST',
      data: 'latitude='+position.coords.latitude+'&longitude='+position.coords.longitude,
      success: function(data) {
        console.log(data);
      },
      error: function(e) {
        //called when there is an error
        //console.log(e.message);
      }
    });
}

请参阅此链接以获取更多信息http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

答案 1 :(得分:1)

以下是如何对结果做一些事情:

function userLocation(cb) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(p){
            // on success
            cb(null, p.coords);
        }, function(e){
            // on error
            cb(e, null);
        });
    } else {
        cb("Geolocation is not supported by this browser.", null);
    }
}

现在调用这个函数:

userLocation(function(err, coords) {
    if (err) console.log(err);
    else {
        // do something with coords
    }
});

答案 2 :(得分:0)

您可以使用getCurrentPosition的第3个参数。 请参考以下示例。

var response = [];
var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

function success(pos) {
  response['latitude'] = pos.coords.latitude;
  response['longitude'] = pos.coords.longitude;
};

function error(err) {
  //
};

navigator.geolocation.getCurrentPosition(success, error, options);

您还可以参考以下链接。

https://www.tutorialspoint.com/html5/geolocation_getcurrentposition.htm

答案 3 :(得分:-2)

尝试使用response.push()

推送当前位置值
var response = [];
if (navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(function(p){

      response.push(p.coords.latitude, p.coords.longitude);

        console.log(response);

    });

} else {
    console.log("Geolocation is not supported by this browser.");
}