2017-06-25 2 views
0

Ich entwickle eine Anwendung, die auf GPS/Benutzer Standort mit dieser tutorial basiert. Die Idee ist, wenn der Benutzer zweite Aktivität öffnet (durch Anklicken der Schaltfläche) sollte das Gerät auf der Karte lokalisieren und sie in jeder Sekunde aktualisiert, hier ist der Aktivitätscode:Benutzerstandort wird nicht auf der Karte angezeigt

public class MapLocation extends AppCompatActivity implements 
    OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 

GoogleMap mGoogleMap; 
GoogleApiClient mGoogleApiClient; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.maplocation); 
    initMap(); 
} 

private void initMap() { 
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment); 
    mapFragment.getMapAsync(this); 
} 

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

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    mGoogleApiClient.connect(); 
} 

LocationRequest mLocationRequest; 

@Override 
public void onConnected(Bundle bundle) { 
    mLocationRequest = LocationRequest.create(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLocationRequest.setInterval(1000); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for Activity#requestPermissions for more details. 
      return; 
     } 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    if(location == null) { 
     Toast.makeText(this, "Can't get current location!", Toast.LENGTH_LONG).show(); 
    } else { 
     LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); 
     CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 15); 
     mGoogleMap.animateCamera(update); 
    } 
}} 

und hier ist der XML-Code:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/_fff"> 

    <fragment 
     android:id="@+id/mapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment" /> 
</LinearLayout> 

und hier sind die notwendigen Berechtigungen:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

die Karte funktioniert gut, aber das Problem ist, dass es mein Gerät wird nicht auf der Karte zeigt (meine Lage), in den tutorial es funktioniert. Ich führe die Anwendung auf dem Gerät und dem Emulator aus und die Ergebnisse sind gleich, die Karte funktioniert einwandfrei, aber mein Standort wird nicht auf der Karte angezeigt. Kann mir jemand erklären, was ich falsch mache?

+0

Vergewissern Sie sich, dass Ihr Emulator GPS/Orte auf zB haben: https: //gyazo.com/577bd3f0c2fb8a9de6b8d1993c7ecc9c –

+0

Es ist auf, die beide auf Emulator und meinem Gerät –

Antwort

0

Ein entscheidender Fehler, den Sie sofort sehen können, ist, dass Sie die setLocationEnabbled(true) Methode vermissen.

Versuchen Sie diesen Code:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    Toast.makeText(this, "onMapReady", Toast.LENGTH_LONG).show(); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
      && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } else { 
     mMap = googleMap; 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setZoomControlsEnabled(true); 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     Toast.makeText(this, "Location is disabled, turn it on in your settings,", Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(this, "onConnected", Toast.LENGTH_LONG).show(); 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      //place marker at current position 
      mMap.clear(); 
      double latitude = mLastLocation.getLatitude(); 
      double longitude = mLastLocation.getLongitude(); 
      LatLng latLng = new LatLng(latitude, longitude); 
      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.position(latLng); 
      markerOptions.title("Current Position"); 
      markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)); 
      mCurrLocation = mMap.addMarker(markerOptions); 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(latitude, longitude)) 
        .zoom(11) 
        .build(); 
      mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     }else{ 
      Toast.makeText(this, "LastLocation is null", Toast.LENGTH_LONG).show();} 

Hoffe, es hilft :)

+0

Sorry für die späte Antwort ... ja, ich habe das vergessen, peinlich. Vielen Dank :) –

Verwandte Themen