0

Ich bin neu bei Android und arbeite durch viele Tutorials, um den aktuellen Standort des Benutzers mit Autozoom zu erhalten und den Standort Latitude und Longitude in einer Textansicht zu speichern. Bis jetzt bin ich nicht in der Lage Location zu bekommen und kontinuierliche Fehler zu bekommen und abzustürzen. Gleichzeitig kann ich den Status von gpsStatus und networkConnectionStatus nicht anzeigen. Ich arbeite mit targetSDKlevel 23.Aktueller Standort in Google Maps und Speichern der empfangenen Position auf TextView

Antworten werden approved.

Heres der Code

public class Maps extends FragmentActivity implements OnMapReadyCallback, LocationListener { 

private GoogleMap mMap; 

public TextView tvRescueLocation; 


void error() 
{ 
    Toast.makeText(getApplicationContext(), "Something went wrong!", Toast.LENGTH_LONG).show(); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    tvRescueLocation = (TextView) findViewById(R.id.tvRescueLocation); 

} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 


    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || 
      !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Location Services Not Active"); 
     builder.setMessage("Please enable Location Services and GPS"); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialogInterface, int i) { 

       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 
      } 
     }); 
     Dialog alertDialog = builder.create(); 
     alertDialog.setCanceledOnTouchOutside(false); 
     alertDialog.show(); 
    } 

    setMap(); 
} 

public void setMap() { 
    try { 
     if (ActivityCompat.checkSelfPermission 
       (this, Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED 
       && 
       ActivityCompat.checkSelfPermission 
         (this, Manifest.permission.ACCESS_COARSE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

      mMap.setMyLocationEnabled(true); 

      //Set map type to TERRAIN 
      mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 

      //Remapping the position of Location button 
      mMap.setPadding(0, 800, 0, 0); 

      //Enable UI controls of Map 
      mMap.getUiSettings().setZoomControlsEnabled(true); 
      mMap.getUiSettings().setCompassEnabled(true); 

      Criteria criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

      String provider = locationManager.getBestProvider(criteria,true); 
      Location newLoc = locationManager.getLastKnownLocation(provider); 

      location(newLoc); 
     } 
     else 
     { 
      finish(); 
     } 
    } 
    catch (Exception e) 
    { 
     error(); 
    } 
} 


@Override 
public void onLocationChanged(Location location) { 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

public void location(Location location) { 
    final double latitude = location.getLatitude(); 
    final double longitude = location.getLongitude(); 
    LatLng latLng = new LatLng(latitude, longitude); 

    mMap.addMarker(new MarkerOptions() 
      .position(latLng) 
      .draggable(true) 
      .icon(BitmapDescriptorFactory 
        .fromResource(R.drawable.ic_location_flag))) 
      .setSnippet("Rescue Location - CLICK HERE"); 


    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom 
      (latLng, 17)); 

    //Adding Custom Circle for Rescue Attention 
    CircleOptions circleOptions = new CircleOptions() 
      .center(latLng) 
      .radius(40) 
      .strokeWidth(3) 
      .strokeColor(Color.RED); 
    mMap.addCircle(circleOptions); 

} 

}

LogCat:

04-01 15:40:29.334 22356-22675/? W/ActivityThread: ClassLoader.loadClass:  The class loader returned by Thread.getContextClassLoader() may fail for  processes that host multiple applications. You should explicitly specify a  context class loader. For example:  Thread.setContextClassLoader(getClass().getClassLoader()); 
04-01 15:40:29.490 22356-22356/? I/Choreographer: Skipped 154 frames! The  application may be doing too much work on its main thread. 
04-01 15:40:29.709 22655-22659/? D/dalvikvm: GC_CONCURRENT freed 732K, 22%  free 4617K/5912K, paused 5ms+10ms, total 43ms 
04-01 15:40:29.709 22655-22655/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 38ms 
04-01 15:40:29.724 22356-22356/? D/AndroidRuntime: Shutting down VM 
04-01 15:40:29.724 22356-22356/? W/dalvikvm: threadid=1: thread exiting with  uncaught exception (group=0x41701ba8) 
04-01 15:40:29.771 22356-22356/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.rescue,  PID: 22356 
                 java.lang.NullPointerException 
                at  com.rescue.Maps.location(Maps.java:159) 
                at  com.rescue.Maps.setMap(Maps.java:125) 
                at  com.rescue.Maps.onMapReady(Maps.java:95) 
                at  com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 
                at  com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source) 
                at  android.os.Binder.transact(Binder.java:361) 
                at  com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82) 
                at  maps.ei.bu$6.run(Unknown Source) 
                at  android.os.Handler.handleCallback(Handler.java:733) 
                at  android.os.Handler.dispatchMessage(Handler.java:95) 
                at  android.os.Looper.loop(Looper.java:136) 
                at  android.app.ActivityThread.main(ActivityThread.java:5059) 
                at  java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                at  dalvik.system.NativeStart.main(Native Method) 
04-01 15:40:29.795 435-707/? W/ActivityManager: Force finishing activity  com.rescue/.Maps 
+0

Check 'logcat' Ausgang, um weitere Informationen über Crash! –

+0

Ich gehe gerade durch den Logcat, aber es gibt keine solche Fehler angezeigt. Sobald ich in der Lage war, den Standort zu bekommen, aber um das GPS und das Netzwerk von dieser Zeit aus zu testen, bekomme ich nicht nur einen Ausnahmefehler. Gibt es eine Chance, .getLastKnownLocation NICHT mit anderen SDK-Level 23 zu verwenden. –

+0

Welches Gerät teste ich? –

Antwort

0

Verwenden Location

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
double longitude = location.getLongitude(); 
double latitude = location.getLatitude(); 

//Now set the value of Double to String 
String Lat = String.valueOf(latitude); 
String Lon = String.valueOf(longitude); 

//Set value to textview 

YourTextView1.setText(Lat); 
YourTextView2.setText(Lon); 
+0

Ich habe das versucht, aber ich bekomme NullPointException, bitte gehen Sie durch meinen vorherigen Kommentar für Details. –

+0

Haben Sie in der Manifestdatei die entsprechenden Berechtigungen erhalten? – Sam

+0

ja

0

getLastKnownLocat ion() hat die Möglichkeit, null zurückzugeben, was dazu führen kann, dass Ihre App mit einer NullPointerException abstürzt.

Alternativ können Sie die Location Strategies docs gehen von Android zur Verfügung gestellt und das Verfahren zur Verfügung gestellt, verwenden:

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager)   this.getSystemService(Context.LOCATION_SERVICE); 
// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
public void onLocationChanged(Location location) { 
    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    // Display a marker on the GoogleMap 
} 
public void onStatusChanged(String provider, int status, Bundle extras) {} 
public void onProviderEnabled(String provider) {} 
public void onProviderDisabled(String provider) {} 
}; 
// Register the listener with the Location Manager to receive location updates 
// NOTE : PLEASE ADJUST THE FREQUENCY OF UPDATES PROPERLY 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
+0

fügen Sie das auf setMap() -Methode, ersetzen Ihr locationmanager Code mit diesem – Sam

+0

löschen Sie Ihren Code von der Methode setMap() vom Standortmanager bis zum öffentlichen void Standort (....) – Sam

+0

Ich habe das versucht, aber es funktioniert nicht richtig, Es startet die Karte, aber es gibt keine Funktionen (Kein AutoZoom, Marker nichts). –

Verwandte Themen