首页 文章

如何以代号1获取当前纬度和经度值

提问于
浏览
3

我试图在Codename One中使用LocationManager获取我当前的纬度和经度值,但我得到的值是针对不同的位置 . 这是我的代码:

LocationManager locationManager = LocationManager.getLocationManager();
Location location = locationManager.getCurrentLocation();
Double loc1= location.getLatitude();
Double loc2= location.getLongitude();
System.out.println("Latitude: "+loc1);
System.out.println("Longitude: "+loc2);

输出:

纬度:40.715353经度:-74.00497299999999

我正确的当前纬度和经度值分别为3.xxxx和101.xxxx .

请问如何从LocationManager获取这些正确的值?

1 回答

  • 3

    这是模拟器上的默认位置 .

    请注意,如果不自行输入,则无法在Simulator上获取实际位置 .

    要将模拟器设置为使用当前位置,请在模拟器上单击 Simulate 并选择 Location Simulation . 输入 Available 的LatLong值,然后单击“更新” . Google Map 会将视图转移到您的位置,您的应用也会如此 .

    您可以在测试应用时保持位置模拟器处于打开状态 .

    另外,如果您的应用需要实时位置,我建议您使用getCurrentLocationAsync() . 以下是一个例子:

    InfiniteProgress ip = new InfiniteProgress();
    Dialog ipDlg = ip.showInifiniteBlocking();
    Location location = LocationManager.getLocationManager().getCurrentLocationSync(30000);
    ipDlg.dispose();
    if (location == null) {
        try {
            location = 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;
        }
    }
    Double loc1 = location.getLatitude();
    Double loc2 = location.getLongitude();
    Log.p("Latitude: " + loc1);
    Log.p("Longitude: " + loc2);
    

相关问题