1

我正在使用 jquery mobile 开发一个 phonegap 应用程序。我没有得到当前位置。在设备上准备好 onSucess 或 onError 功能不会被触发。我想只有我没有得到位置。这是我的代码。

function onDeviceReady() {
          var options = { frequency: 3000 };
          watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);              }
    function  onSuccess(position){
       var minZoomLevel = 6;
       map = new google.maps.Map(document.getElementById('map_canvas'), {
                  zoom: minZoomLevel;
           center: new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
           mapTypeId: google.maps.MapTypeId.ROADMAP
       });

        var marker = new google.maps.Marker({
         position: new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
         map: map,
        });  
     }
    function onError() {
        alert("Sorry,we cann't find your location");
    }
4

5 回答 5

1

我正在开发的一些应用程序也有同样的问题。此问题/解决方案是某些最新版本的 phonegap 上的 android 设备所特有的。我现在不记得我在哪里找到了它,但它对我有用:

不要指定 onError 回调,将调用包装在 try catch 中并使用它来处理 onError 场景

try{
    navigator.geolocation.getCurrentPosition(function (position) {
       // If the position can be found send the current location to order the deals
       doSomething(position.coords.longitude, position.coords.latitude);
    });
}
catch(err){ doSomethingWithoutCoordinates(); }

似乎当你给 phonegap 一个错误选项时,它会在没有尝试成功的情况下使用它。

免责声明:我不能肯定会调用 catch ,这对我有用,并且总是给我一个坐标值,所以你可能想在将它投入生产之前彻底测试。

于 2014-01-22T18:06:25.617 回答
1

请试试这个

var options =  { maximumAge: 3000, timeout: 3000, enableHighAccuracy: true };
navigator.geolocation.getCurrentPosition(success, navFail,options);

如果您使用的是 android,则必须使用“enableHighAccuracy: true”

谢谢

于 2014-03-21T23:27:48.710 回答
0
**Phonegap android google map with geolocation** 

**First add file in html page**
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>   

Add in javascript 

$(document).ready(function(){
navigator.geolocation.getCurrentPosition(onSuccess, onError);          
 });

function onSuccess(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;

    var secheltLoc = new google.maps.LatLng(lat, lng);

var myMapOptions = {
     zoom: 16
    ,center: secheltLoc
    ,mapTypeId: google.maps.MapTypeId.HYBRID
};
var theMap = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);
var image = "img/map_pin.png"
var marker = new google.maps.Marker({
    map: theMap,
    draggable: false,
    position: new google.maps.LatLng(lat, lng),
    visible: true,
    icon: image,
    title: Title // Title
});

var infowindow = new google.maps.InfoWindow({
    content: contentString
});
infowindow.open(theMap,marker); 
google.maps.event.addListener(marker, "click", function (e) {
    infowindow.open(theMap,marker);     
});

}

 /** onError CALLBACK RECEIVES A POSITIONERROR OBJECT **/
 function onError(error)  
 {  

switch(error.code)  
{  
    case error.PERMISSION_DENIED: alert(MSG_PERMISSION_DENIED);  
    break;  

    case error.POSITION_UNAVAILABLE: alert(MSG_POSITION_UNAVAILABLE);  
    break;  

    case error.TIMEOUT: alert(MSG_TIMEOUT);  
    break;  

    default: 
        alert(MSG_UNKNOWN);  
    break;  
} 

  }
于 2013-11-08T12:39:27.567 回答
0

好的,这对我有用:

  1. 确保您的设备或模拟器启用了 GPS。

  2. 确保您安装了地理定位插件(运行phonegap local plugin add org.apache.cordova.geolocation

  3. 添加超时并设置 enableHighAccuracy:

    navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 10000, enableHighAccuracy: true});
    

    在某些模拟器中,您需要将 enableHighAccuracy 设置为 false,因此请尝试如果仍然不起作用。

  4. 如果您使用的是 Android 模拟器,它不会读取 GPS 值,因此我们需要通过命令行发送它们。我们需要在模拟器运行的端口中启动一个 telnet 会话(你可以在模拟器窗口标题中查看端口,开头的数字,在我的例子中是 5554):

    telnet localhost 5554
    

然后运行命令

    geo fix -122.4 37.78

如果您关闭应用程序,您需要重新发送地理位置,所以如果它不起作用,只需geo fix在打开应用程序后,在超时事件触发之前运行命令。

于 2014-04-18T11:18:52.780 回答
0

请像这样使用(确保您的设备位置设置为ON);

navigator.geolocation.getCurrentPosition(onSuccess,onError,{enableHighAccuracy:true});

当我像这样使用它时,它可以在 IOS 上运行,但不能在 Android 设备上运行;

{ maximumAge: 3000, timeout: 3000, enableHighAccuracy: true }
于 2015-01-01T21:23:16.597 回答