我正在尝试制作Google Map应用程序,但是当我在android studio模拟器上运行它时,模拟器会停止它,尽管我在手机上运行它the error
............................................... ................................................... ................................................... ................................................... .........................................
代码
package com.example.amr.googleplaces;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
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.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Map is ready");
mMap = googleMap;
if (mLocationPermissionGranted) {
getDeviceLocation();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
return;
}
mMap.setMyLocationEnabled(true);
}
}
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION= Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private boolean mLocationPermissionGranted = false;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationProviderClient;
private static final float DEFAULT_ZOOM = 15F;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocationPermission();
}
private void getDeviceLocation()
{
Log.d(TAG,"getting the device location");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try
{
if(mLocationPermissionGranted)
{
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener()
{
@Override
public void onComplete(@NonNull Task task)
{
if(task.isSuccessful())
{
Log.d(TAG,"location found");
Location currentlocation = (Location)task.getResult();
moveCamera(new LatLng(currentlocation.getLatitude(),currentlocation.getLongitude()),DEFAULT_ZOOM);
}
else
{
Log.d(TAG,"current location is null");
Toast.makeText(MapActivity.this,"unable to find location",Toast.LENGTH_SHORT).show();
}
}
});
}
}
catch(SecurityException e)
{
Log.e(TAG,"security exception" + e.getMessage());
}
}
private void moveCamera(LatLng latLng,float zoom )
{
Log.d(TAG,"moving camera to latitude : "+ latLng.latitude +"longtitiude"+latLng.longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,zoom));
}
private void initMap()
{
Log.d(TAG,"initializing map");
SupportMapFragment mapFrafment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFrafment.getMapAsync(MapActivity.this);
}
private void getLocationPermission()
{
Log.d(TAG,"get location permission");
String[] permissions = {FINE_LOCATION,COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
{
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED)
{
mLocationPermissionGranted = true;
initMap();
}
else
{
ActivityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
}
}
else
{
ActivityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
Log.d(TAG,"onRequestPermissionsResult called");
mLocationPermissionGranted = false;
switch(requestCode)
{
case LOCATION_PERMISSION_REQUEST_CODE:
if(grantResults.length>0 )
{
for (int i = 0 ; i< grantResults.length;i++)
{
if(grantResults[i] != PackageManager.PERMISSION_GRANTED)
{
mLocationPermissionGranted = false;
Log.d(TAG,"permission failed");
return;
}
}
Log.d(TAG,"onRequestPermissionsResult :permission granted");
mLocationPermissionGranted = true;
initMap();
}
}
}
}
答案 0 :(得分:0)
确保您的模拟器具有
还要确保您的Google API密钥正常工作。
还要确保您在manifest.xml
上有这个
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />