2016-08-04 25 views

回答

0

不太清楚你問在這裏,但離子不能給你geolocations,科爾多瓦但是可以。如果你想讓自己變得容易,你應該看看ngCordova,它是一個cordova插件的角度包裝。看看這裏:http://ngcordova.com/docs/plugins/geolocation/

1

這是一個例子,如何使用長https://github.com/apache/cordova-plugin-geolocation

.controller('MyCtrl', function($scope, $cordovaGeolocation) { 
    var posOptions = {timeout: 10000, enableHighAccuracy: false}; 
    $cordovaGeolocation 
    .getCurrentPosition(posOptions) 

    .then(function (position) { 
     var lat = position.coords.latitude 
     var long = position.coords.longitude 
     console.log(lat + ' ' + long) 
    }, function(err) { 
     console.log(err) 
    }); 

    var watchOptions = {timeout : 3000, enableHighAccuracy: false}; 
    var watch = $cordovaGeolocation.watchPosition(watchOptions); 

    watch.then(
     null, 

     function(err) { 
     console.log(err) 
     }, 

     function(position) { 
     var lat = position.coords.latitude 
     var long = position.coords.longitude 
     console.log(lat + '' + long) 
     } 
    ); 

    watch.clearWatch(); 

}) 

你也注意到posOptions和watchOptions對象,以得到緯度。我們使用超時來調整允許以毫秒傳遞的最大時間長度,並且enableHighAccuracy設置爲false。它可以設置爲true以獲得最佳結果,但有時可能會導致一些錯誤。還有maximumAge選項可用於顯示接受的舊位置。它使用毫秒,與超時選項相同。

當我們啓動我們的應用程序並打開控制檯時,它會記錄設備的經度和緯度。當我們的位置發生變化時,緯度和長度值將會改變。

2

您可以使用對URL API的調用返回JSON對象,然後獲取經度和緯度。以下示例調用http://freegeoip.net/json/並打印出結果。要訪問緯度和經度,您可以使用result.latituderesult.longitude字段。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<script> 
 
$(document).ready(function(){ 
 
    $("button").click(function(){ 
 
     $.getJSON("http://freegeoip.net/json/", function(result){ 
 
      $.each(result, function(i, field){ 
 
       $("div").append(field + " "); 
 
      }); 
 
     }); 
 
    }); 
 
}); 
 
</script> 
 
</head> 
 
<body> 
 

 
<button>Get JSON data</button> 
 

 
<div></div> 
 

 
</body> 
 
</html>

相關問題