我正在使用codename one开发iOS应用。我想获取当前位置并通过短信发送。
我从Java Android Studio获得此代码,我不知道如何获取当前位置并检查GPS是否已打开。
我在下面试过,但没有成功(我不确定他们如何启动GPS并获得位置)
obstacle.layer.transform = CATransform3DMakeScale(-1,1,1);
onLocationChanged方法:
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabledGPS) {
//alert GPS is off
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
Toast.makeText(this, "Provider: " + provider, Toast.LENGTH_SHORT).show();
onLocationChanged(location);
} else {
//do something
}
答案 0 :(得分:3)
你不能用codenameone启动GPS,你只能检查它是否已打开并显示一条消息,如果不是。
尝试以下代码:
//Check if location is turned on and your app is allowed to use it.
if (Display.getInstance().getLocationManager().isGPSDetectionSupported()) {
if (Display.getInstance().getLocationManager().isGPSEnabled()) {
InfiniteProgress ip = new InfiniteProgress();
final Dialog ipDlg = ip.showInifiniteBlocking();
//Cancel after 20 seconds
Location loc = LocationManager.getLocationManager().getCurrentLocationSync(20000);
ipDlg.dispose();
if (loc != null) {
double lat = loc.getLatitude();
double lng = loc.getLongitude();
try {
Display.getInstance().sendSMS("09123456789", "http://maps.google.com/?q=" + lat + "," + lng, false);
} catch (IOException ex) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
ex.printStackTrace();
}
} else {
Dialog.show("GPS error", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null);
}
} else {
Dialog.show("GPS disabled", "AppName needs access to GPS. Please enable GPS", "Ok", null);
}
} else {
InfiniteProgress ip = new InfiniteProgress();
final Dialog ipDlg = ip.showInifiniteBlocking();
//Cancel after 20 seconds
Location loc = LocationManager.getLocationManager().getCurrentLocationSync(20000);
ipDlg.dispose();
if (loc != null) {
double lat = loc.getLatitude();
double lng = loc.getLongitude();
try {
Display.getInstance().sendSMS("09123456789", "http://maps.google.com/?q=" + lat + "," + lng, false);
} catch (IOException ex) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
ex.printStackTrace();
}
} else {
Dialog.show("GPS error", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null);
}
}