简体   繁体   English

如何获取经纬度坐标?

[英]How to get lat and lon geolocation coordinates?

I have app shows your current location via the lat and lon coordinates and displays on the map. 我的应用程序通过纬度和经度坐标显示您当前的位置,并在地图上显示。 Also in this application has a function display markers denoting sights. 在该应用程序中还具有表示景点的功能显示标记。 These markers I get from api wiki using lat and lon coordinates. 这些标记是我使用lat和lon坐标从api Wiki获得的。 But the problem is that the coordinates at the moment are completely static and must be write into the code yourself. 但是问题在于,目前的坐标是完全静态的,必须自己将其写入代码中。 How I get lat and lon dynamically from a third-party service? 如何从第三方服务动态获取经纬度? For example, from JSON IP API 例如,来自JSON IP API

Code that is currently responsible for retrieving data 当前负责检索数据的代码

app.factory('places', ['$http', function($http) {

    return $http.jsonp('https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=5000&gscoord=' + 43.1056 + '%7C' + 131.8735 + '&gslimit=30&format=json&callback=JSON_CALLBACK')
        .success(function(data) {
            return data
        })
        .error(function(err) {
            return err
        })

}])

I tried to make it so 我试图做到

app.factory('places', ['$http', '$scope', function($http, $scope) {

  $scope.coordinates = {};
  $http.get('http://ip-api.com/json')
    .success(function(data) {
      $scope.coordinates.lat = data.lat;
      $scope.coordinates.lon = data.lon;
    });

    return $http.jsonp('https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=5000&gscoord=' + coordinates.lat + '%7C' + coordinates.lon + '&gslimit=30&format=json&callback=JSON_CALLBACK')
        .success(function(data) {
            return data
        })
        .error(function(err) {
            return err
        })

}])

and

app.factory('places', ['$http', function($http) {

  $http.get('http://ip-api.com/json')
    .success(function(coordinates) {
         return $http.jsonp('https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=5000&gscoord=' + coordinates.lat + '%7C' + coordinates.lon + '&gslimit=30&format=json&callback=JSON_CALLBACK')
                .success(function(data) {
                    return data
                })
                .error(function(err) {
                    return err
                })
    });

}])

but its not working. 但它不起作用。 Could you help me with this problem? 您能帮我解决这个问题吗? If you need to see the entire code of the application is completely you can download it here https://www.dropbox.com/s/qiu91hhdrc0qy5o/GeoApp.rar?dl=0 如果您需要查看应用程序的完整代码,可以在这里下载https://www.dropbox.com/s/qiu91hhdrc0qy5o/GeoApp.rar?dl=0

You can use HTML5 feature of the browsers and simply write a function to get the current coordinates: 您可以使用浏览器的HTML5功能,只需编写一个函数即可获取当前坐标:

var deferred;

$scope.currentPosition = function() {
    deferred = $q.defer();

    if (window.navigator && window.navigator.geolocation) {
        window.navigator.geolocation.getCurrentPosition(currentPosition, handleErrorOnTrackCurrentPosition);
    } else {
        deferred.reject({msg: "Browser does not supports HTML5 geolocation"});
    }
    return deferred.promise;
};

function currentPosition(position) {
    deferred.resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
}

function handleError(error) {
    deferred.reject({msg: error.message});
}

Now, when you want to get current coordinates, simply write: 现在,当您想获取当前坐标时,只需编写:

$scope.currentPosition().then(function(data) {
    console.log(data.latitude, data.longitude);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM