2017-07-04 132 views
0

我嘗試使用當前位置在我的MapActivity上設置標記。如果我使用模擬器,地圖不會有任何標記。如果我使用我的移動設備(API 24),則該活動在沒有標記的情況下短時間啓動,然後應用程序崩潰。 感謝您的幫助。Android Studio - 當前位置 - Google地圖

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

我可以驗證您是否在使用帶有Google Play APIs支持的模擬設備? –

回答

0

你應該調用get位置onMapReady

取下的onCreate獲得位置的所有邏輯和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

檢查這個代碼的工作對我來說

​​
+0

我不能使用@Override Methode onConnected(Bundle bundle)。另外我不能使用LocationServices。 – Jade

+0

將此添加到您的活動公共類MapsActivity擴展FragmentActivity實現OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener –

+0

好的應用程序不會再崩潰,但仍然沒有在地圖上的標記。我用你的代碼和Android Studio在「setMyLocationEnabled」和Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);我實現了它,但沒有效果 – Jade

相關問題