在 iOS 台设备上测试定位服务
Testing Location Services on iOS Device
我正在构建一个应用程序,它将列出距离用户当前位置最近的产品经销商。
在我的设备上测试时 iPhone 6 & iPhone 4S 我可以看到我的应用没有获取位置的权限但是当我进入设置时我看不到我的列出测试应用程序以授予它该权限。
这是因为 "Run" 通过 Appcelerator Studio 安装应用程序的方式吗?请问如何授予测试应用权限?
我的代码是:
function getCurrentPhoneLocation(callback)
{
Titanium.API.info("get phone location " + Ti.Geolocation.locationServicesEnabled);
if(Ti.Geolocation.locationServicesEnabled)
{
Titanium.API.info("GPS permissions: " + Ti.Geolocation.locationServicesAuthorization + " (" + Ti.Geolocation.AUTHORIZATION_ALWAYS + " | " + Ti.Geolocation.AUTHORIZATION_AUTHORIZED + " | " + Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE + ")");
if (Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_ALWAYS || Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_AUTHORIZED || Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE)
{
Titanium.API.info("Got permissions - lets go!");
Ti.Geolocation.purpose = 'Get current location';
var currentPhoneLocation = {};
Ti.Geolocation.getCurrentPosition(function(e){
Titanium.API.info("from pos: " + JSON.stringify(e));
if(e.success === false) {
Ti.API.error('Error:' + e.error);
alert("Location is currently unavailable");
callback( false );
} else {
currentPhoneLocation.longitude = e.coords.longitude;
currentPhoneLocation.latitude = e.coords.latitude;
Ti.API.info("Returned Cords: " + JSON.stringify(currentPhoneLocation));
callback();
}
});
}
else
{
Titanium.API.info("No APP permission");
Titanium.UI.createAlertDialog({title:'Location Service', message:'Please grant this app permission to get your location.'}).show();
callback( false );
}
}
else
{
Titanium.API.info("No GPS available");
Titanium.UI.createAlertDialog({title:'Location Service', message:'Please turn on your location services.'}).show();
callback( false );
}
}
您可以看到我的跟踪代码显示 Ti.Geolocation.locationServicesAuthorization 正在返回“0”。
[INFO] : get phone location true
[INFO] : GPS permissions: 0 (3 | 3 | 4)
[INFO] : No APP permission
[INFO] : Recieved location: false
无论您如何安装,首选项都是一样的。你还是得授权一下。
但是,最新版本中的权限发生了很多变化,我建议查看文档以正确设置它。你可以找到一个 example app on GitHub
检查是否已被授予的最佳方法是:
Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS);
此外,我建议获取位置。假的其实是你提供的。如果您授予应用程序权限(在设置中验证),这应该有效
Ti.GeoLocation.getCurrentPosition(callback(e){})
我正在构建一个应用程序,它将列出距离用户当前位置最近的产品经销商。
在我的设备上测试时 iPhone 6 & iPhone 4S 我可以看到我的应用没有获取位置的权限但是当我进入设置时我看不到我的列出测试应用程序以授予它该权限。
这是因为 "Run" 通过 Appcelerator Studio 安装应用程序的方式吗?请问如何授予测试应用权限?
我的代码是:
function getCurrentPhoneLocation(callback)
{
Titanium.API.info("get phone location " + Ti.Geolocation.locationServicesEnabled);
if(Ti.Geolocation.locationServicesEnabled)
{
Titanium.API.info("GPS permissions: " + Ti.Geolocation.locationServicesAuthorization + " (" + Ti.Geolocation.AUTHORIZATION_ALWAYS + " | " + Ti.Geolocation.AUTHORIZATION_AUTHORIZED + " | " + Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE + ")");
if (Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_ALWAYS || Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_AUTHORIZED || Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE)
{
Titanium.API.info("Got permissions - lets go!");
Ti.Geolocation.purpose = 'Get current location';
var currentPhoneLocation = {};
Ti.Geolocation.getCurrentPosition(function(e){
Titanium.API.info("from pos: " + JSON.stringify(e));
if(e.success === false) {
Ti.API.error('Error:' + e.error);
alert("Location is currently unavailable");
callback( false );
} else {
currentPhoneLocation.longitude = e.coords.longitude;
currentPhoneLocation.latitude = e.coords.latitude;
Ti.API.info("Returned Cords: " + JSON.stringify(currentPhoneLocation));
callback();
}
});
}
else
{
Titanium.API.info("No APP permission");
Titanium.UI.createAlertDialog({title:'Location Service', message:'Please grant this app permission to get your location.'}).show();
callback( false );
}
}
else
{
Titanium.API.info("No GPS available");
Titanium.UI.createAlertDialog({title:'Location Service', message:'Please turn on your location services.'}).show();
callback( false );
}
}
您可以看到我的跟踪代码显示 Ti.Geolocation.locationServicesAuthorization 正在返回“0”。
[INFO] : get phone location true
[INFO] : GPS permissions: 0 (3 | 3 | 4)
[INFO] : No APP permission
[INFO] : Recieved location: false
无论您如何安装,首选项都是一样的。你还是得授权一下。
但是,最新版本中的权限发生了很多变化,我建议查看文档以正确设置它。你可以找到一个 example app on GitHub
检查是否已被授予的最佳方法是:
Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS);
此外,我建议获取位置。假的其实是你提供的。如果您授予应用程序权限(在设置中验证),这应该有效
Ti.GeoLocation.getCurrentPosition(callback(e){})