2017-07-04 2 views
0

Ich versuche, eine Markierung auf meiner MapActivity mit meiner aktuellen Position zu setzen. Wenn ich den Emulator verwende, hat die Karte keinen Marker. Wenn ich mein mobiles Gerät (API 24) verwende, startet die Aktivität für kurze Zeit ohne Marker und dann stürzt die App ab. Vielen Dank für Ihre Hilfe Jungs.Android Studio - aktuelle Position - Google Map

import android.content.pm.PackageManager; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.io.IOException; 
import java.util.List; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    private LocationManager locationManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_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); 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     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; 
     } 
     if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 
        double latitude = location.getLatitude(); 
        double longitude = location.getLongitude(); 
        LatLng latLng = new LatLng(latitude, longitude); 
        Geocoder geocoder = new Geocoder(getApplicationContext()); 
        try { 
         List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); 
         String str = addressList.get(0).getLocality(); 
         str += addressList.get(0).getCountryName(); 
         mMap.addMarker(new MarkerOptions().position(latLng).title(str)); 
         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onStatusChanged(String s, int i, Bundle bundle) { 

       } 

       @Override 
       public void onProviderEnabled(String s) { 

       } 

       @Override 
       public void onProviderDisabled(String s) { 

       } 
      }); 
     } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 
        double latitude = location.getLatitude(); 
        double longitude = location.getLongitude(); 
        LatLng latLng = new LatLng(latitude, longitude); 
        Geocoder geocoder = new Geocoder(getApplicationContext()); 
        try { 
         List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); 
         String str = addressList.get(0).getLocality(); 
         str += addressList.get(0).getCountryName(); 
         mMap.addMarker(new MarkerOptions().position(latLng).title(str)); 
         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onStatusChanged(String s, int i, Bundle bundle) { 

       } 

       @Override 
       public void onProviderEnabled(String s) { 

       } 

       @Override 
       public void onProviderDisabled(String s) { 

       } 
      }); 
     } 
    } 

    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
//  mMap = googleMap; 
//  mMap.getMaxZoomLevel(); 
//  // Add a marker in Sydney and move the camera 
//  LatLng posi = new LatLng(100, 100); 
//  mMap.addMarker(new MarkerOptions().position(posi).title("my position")); 
//  mMap.moveCamera(CameraUpdateFactory.newLatLng(posi)); 

    } 

} 
+0

Kann ich überprüfen, ob Sie ein emuliertes Gerät mit 'Google Play APIs'-Unterstützung verwenden? –

Antwort

0

Sie sollten in onMapReady erhalten Lage nennen

aller Logik entfernen Standort aus onCreate bekommen und fügen Sie folgenden Code in onMapReady

@Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     if (mMap!=null){ 
      Log.d(TAG, "onMapReady: Map is "+mMap); 
     }else { 
      Log.d(TAG, "onMapReady: Map is not "+mMap); 
     } 
     mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { 
      @Override 
      public boolean onMyLocationButtonClick() { 
//    Toast.makeText(getActivity(), "Map button click"+mMap.getMyLocation().getLongitude(), Toast.LENGTH_SHORT).show(); 

       try { 
        displayLocationSettingsRequest(getContext()); 
        latLngs=new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude()); 
        setLocationName(latLngs); 
       } catch (Exception e) { 
        e.printStackTrace(); 
        Log.d(TAG, "onMyLocationButtonClick: "+e); 
       } 

       //check permission 
       return false; 
      } 
     }); 
     markerOptions=new MarkerOptions().position(new LatLng(-34, 151)); 
     mMarker=mMap.addMarker(markerOptions); 
} 
0

Scheck diesen Code seine für mich arbeiten

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 
    //buildGoogleApiClient(); 

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

} 


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

    // Add a marker in Sydney and move the camera 
    mMap.setMyLocationEnabled(true); 

    //add this here: 
    buildGoogleApiClient(); 

    //LatLng loc = new LatLng(lat, lng); 
    //mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); 
    //mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     lat = mLastLocation.getLatitude(); 
     lng = mLastLocation.getLongitude(); 

     LatLng loc = new LatLng(lat, lng); 
     mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); 
    } 
} 
+0

Ich kann die @Override Methode onConnected (Bundle Bundle) nicht verwenden. Außerdem kann ich LocationServices nicht verwenden. – Jade

+0

dies zu Ihrer öffentlichen Aktivität hinzufügen MapsActivity extends FragmentAktivität implementiert OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener –

+0

Ok, die App stürzt nicht mehr ab, aber immer noch keine Markierungen auf der Karte. Ich habe Ihren Code und Android Studio um Erlaubnis gefragt zu überprüfen bei "setMyLocationEnabled" und Standort mlaStlocation = LocationServices.FusedLocationApi.getLastLocation (mGoogleApiClient); Ich habe es implementiert, aber es gibt keine Auswirkungen – Jade

Verwandte Themen