如何检查位置服务是否启用在Appcelerator

问题描述:

您可以请帮助我检查位置服务是否启用或不在Appcelerator中。 我正在与Titanium SDk 6.1.2和三星S5搭配Marshmellow操作系统。即使GPS已启用/未启动,但每次都会导致错误。 在此先感谢。如何检查位置服务是否启用在Appcelerator

首先,您需要检查位置权限适用于Android的应用程序&那么您需要检查是否在设备中启用位置服务。

两者都是不同的陈述。

第一个检查应用程序访问位置的权限&第二个是关于检查位置服务是打开还是关闭。

,不检查位置许可首先在Android上,你无法检查位置开/关状态,否则它将总是导致状态。

首先在iOS中添加这tiapp.xml - >的plist - >字典

<key>NSLocationAlwaysUsageDescription</key> 
<string>Determine Current Location</string> 

现在,这里是为Android/iOS的跨兼容的代码。现在

function checkLocationEnabledOrNot(_callback, _args) { 
    if (Titanium.Geolocation.locationServicesEnabled) { 
     _callback(_args); 

    } else { 
     alert("Turn on location on your device."); 
    } 
} 


// pass _callback method you want to call after successful access to location 
// you can also pass arguments as 2nd parameter to the function you want to call 

function startLocationProcess(_callback, _args) { 
    Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_HIGH; 

    if (OS_IOS) { 
     checkLocationEnabledOrNot(_callback, _args); 

    } else if (OS_ANDROID) { 
     if (Ti.Geolocation.hasLocationPermissions()) { 
      checkLocationEnabledOrNot(_callback, _args); 

     } else { 
      Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function (locationEvent) { 
       if (locationEvent.success) { 
        checkLocationEnabledOrNot(_callback, _args); 

       } else { 
        alert("Location permissions are required to access locations."); 
       } 
      }); 
     } 
    } 
} 

,在任何你想要的位置签到后做一个点击按钮,你可以简单地做这样的:

function anotherFunction(name) { 
    alert(name); 
} 


$.someButton.addEventListener('click', function (e) { 
    startLocationProcess(anotherFunction, "Hello D.Ish"); 
});