2017-03-13 7 views
2

Ich habe eine Google Map. Im Simulator funktioniert alles gut, aber in Android-Gerät findet nie den aktuellen Speicherort (zeigt die Nachricht Location Error - siehe Code). Die Karte öffnet sich in der Weltansicht ...Codenameone google map

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

Das GPS ist auf dem Gerät aktiv. Ich kann meinen Standort in anderen Apps sehen.

Antwort

1

Sie können den Code durch Rückgriff auf einige Standardpositionen verbessern, wenn die GPS-Empfang in einem bestimmten Bereich nicht gut ist:

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

Dank. Der Standardspeicherort hat funktioniert. Es dauert sehr lange, das Formular mit der Karte zu öffnen ... 20/30 sec ... ist das normal? wenn ich zurück gehe Form und wieder eingeben dauert noch mehr Zeit ... es zeigt den unendlichen Fortschritt, so denke ich, es dauert sehr lange, um den Ort zu finden ... – atmenezes

+0

Ich vergaß zu erwähnen, dass dieser Prozess in 'erfolgen sollte postShow() 'und nicht in' beforeShow() ' – Diamond

+1

Vermeiden Sie auch InfiniteBlocking, es ärgert Benutzer. Normalerweise füge ich InfiniteProgress oben rechts oder anderswo zur Symbolleiste hinzu und entferne es, sobald der Vorgang abgeschlossen ist. – Diamond