【问题标题】:Codename one GPS provider and current location代号一 GPS 提供商和当前位置
【发布时间】:2016-02-01 02:43:11
【问题描述】:

我正在使用 codename one 开发 iOS 应用程序。我想获取当前位置并通过短信发送。

我从 Java Android Studio 得到这段代码,我不知道如何获取当前位置以及检查 GPS 是否打开。

我在下面尝试过,但没有成功(我不确定他们如何启动 GPS 并获取位置)

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 方法:

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();
}

【问题讨论】:

    标签: java ios gps location codenameone


    【解决方案1】:

    你不能在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);
        }
    }
    

    【讨论】:

    • 此时,我没有收到错误,但什么也没发生。没有发送短信,也没有对话框警告。我打开了 GPS
    • 您在哪个设备上尝试这个?尝试将此Display.getInstance().sendSMS("09123456789", "http://maps.google.com/?q=" + lat + "," + lng, false); 更改为此Display.getInstance().sendSMS("09123456789", "http://maps.google.com/?q=" + lat + "," + lng, true);
    • 我使用的是三星 Xcover 2 (android) 该应用程序应该可以在 IOS 上运行,但我想我可以先在任何设备上对其进行测试
    • 如果您对其进行了任何更改以使其正常工作,您可以编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多