代号一个GPS提供商和当前位置 [英] Codename one GPS provider and current location
本文介绍了代号一个GPS提供商和当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用的iOS应用程序。我想获取当前位置并通过短信发送。
I am working on an iOS app with codename one. I want to get the current location and send it by SMS.
我从Java Android Studio获得了此代码,我不知道如何获取当前位置,检查GPS是否打开。
I got this code from Java Android Studio, I don't know how to get the current location and also check if GPS is turned on.
我在下面尝试,但没有成功(我不知道他们是如何启动GPS并获取位置的)
I tried below, but without success (I'm not sure how they start the GPS and get the location)
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
}
< onLocationChanged方法:
The onLocationChanged method:
try {
StringBuffer smsBody = new StringBuffer();
smsBody.append("http://maps.google.com/?q=");
smsBody.append(gpsLocation.getLatitude());
smsBody.append(",");
smsBody.append(gpsLocation.getLongitude());
String phnum="xxxxx";
String smsbod= smsBody.toString();
Display.getInstance().sendSMS(phnum,smsbod);
} catch (IOException ex) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
ex.printStackTrace();
}
推荐答案
您无法启动在codenameone中的GPS,你只能检查它是否打开,如果不是,显示一条消息。
You can't start the GPS in codenameone, you can only check if it's turned on and display a message if it's not.
请尝试以下代码:
Try the code below:
//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);
}
}
这篇关于代号一个GPS提供商和当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文