atmenezes
atmenezes

Reputation: 43

Codenameone google map

I have a google map. In the simulator, everything works fine but in android device never finds the current location (shows message Location Error - see code). The map opens in world view...

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

The GPS is active on the device. I can see my location in other apps.

Upvotes: 2

Views: 84

Answers (1)

Diamond
Diamond

Reputation: 7483

You can improve your code by falling back to some default locations if GPS reception is not good in a particular area:

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

Upvotes: 1

Related Questions