【问题标题】:Codenameone google mapCodenameone 谷歌地图
【发布时间】:2017-03-14 13:35:26
【问题描述】:

我有一个谷歌地图。在模拟器中,一切正常,但在 android 设备中永远找不到当前位置(显示消息位置错误 - 请参阅代码)。地图以世界视图打开...

cnt = new MapContainer(new GoogleMapsProvider(""));
cnt.setMapType(MAP_TYPE_TERRAIN);

LocationManager locationManager = LocationManager.getLocationManager();

InfiniteProgress ip = new InfiniteProgress();
Dialog ipDlg = ip.showInifiniteBlocking();
Location loc = LocationManager.getLocationManager().getCurrentLocationSync(30000);
ipDlg.dispose();
if (loc == null) {
    try {
        loc = LocationManager.getLocationManager().getCurrentLocation();
    } catch (IOException err) {
        Dialog.show("Location Error", "Unable to find your current location, please be sure that your GPS is turned on", "OK", null);
        return;
    }
}
locationManager.setLocationListener(this);

if (loc != null) {
    double lat = loc.getLatitude();
    double lng = loc.getLongitude();
    Coord coordinate = new Coord(lat, lng);
}

设备上的 GPS 处于活动状态。 我可以在其他应用中查看我的位置。

【问题讨论】:

    标签: java codenameone


    【解决方案1】:

    如果特定区域的 GPS 接收不佳,您可以通过回退到一些默认位置来改进您的代码:

    cnt = new MapContainer(new GoogleMapsProvider(""));
    cnt.setMapType(MAP_TYPE_TERRAIN);
    
    LocationManager locationManager = LocationManager.getLocationManager();
    Location loc = locationManager.getLastKnownLocation(); //default
    if (locationManager.isGPSDetectionSupported()) {
        if (locationManager.isGPSEnabled()) {
            InfiniteProgress ip = new InfiniteProgress();
            Dialog ipDlg = ip.showInifiniteBlocking();
            Location loc2 = locationManager.getCurrentLocationSync(30000);
            if (loc2 != null) {
                loc = loc2;
            }
        } else {
            Dialog.show("Location Error", "Unable to find your current location, please be sure that your GPS is turned on", "OK", null);
        }
    } else {
        InfiniteProgress ip = new InfiniteProgress();
        Dialog ipDlg = ip.showInifiniteBlocking();
        Location loc2 = locationManager.getCurrentLocationSync(30000);
        if (loc2 != null) {
            loc = loc2;
        } else {
            loc = LocationManager.getLocationManager().getCurrentLocation();
        }
    }
    if (loc != null) {
        double lat = loc.getLatitude();
        double lng = loc.getLongitude();
        Coord coordinate = new Coord(lat, lng);
    }
    

    【讨论】:

    • 谢谢。默认位置有效。用地图打开表格需要很长时间...20/30秒...正常吗?如果我返回表格并重新输入需要更多时间......它显示了无限的进步,所以我认为找到位置需要很长时间......
    • 我忘了说这个过程应该在postShow()而不是beforeShow()完成
    • 另外,避免使用 InfiniteBlocking,它会惹恼用户。我通常做的是将 InfiniteProgress 添加到工具栏右上角或其他任何地方,并在该过程完成后将其删除。
    • 我有一个类扩展表单,所以代码在 start() 中。创建地图后,我更改了代码以显示表单 (this.show() )。现在它在世界视图中打开表单并花费时间显示位置...花费很长时间才能获取位置是否正常?
    • 在这种情况下,将上面的代码包装在 callSerially()... 类似 Display.getInstance().callSerially(() ->{ //above code}); 或表单的 showListener 中...form.addShowListener()
    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多