Android 已于4月27日获得';t显示地图

Android 已于4月27日获得';t显示地图,android,google-maps,Android,Google Maps,我有两个几乎相同代码的项目,但这一个不会显示地图,相反,它只会烤我放在地图里的东西 已经 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); @Override public void onMapReady(GoogleMap goo

我有两个几乎相同代码的项目,但这一个不会显示地图,相反,它只会烤我放在地图里的东西

已经

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(MapsActivity.this, "onMapReady", Toast.LENGTH_SHORT).show();
    mMap = googleMap;

    LatLng start = new LatLng(10.008154, 123.635460);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(start, 9));
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    } else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }
}
protected synchronized void buildGoogleApiClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
我有一个公共类,它扩展了FragmentActivity并实现了OnMapReadyCallback

 private GoogleMap mMap;
让我的谷歌地图私有化,因为它在我的另一个项目中起作用

在我的onCreate中,后面是onMapReady

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(MapsActivity.this, "onMapReady", Toast.LENGTH_SHORT).show();
    mMap = googleMap;

    LatLng start = new LatLng(10.008154, 123.635460);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(start, 9));
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    } else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }
}
protected synchronized void buildGoogleApiClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
Toast.makeText(MapsActivity.this,“onMapReady”,Toast.LENGTH_SHORT.show();
mMap=谷歌地图;
LatLng start=新LatLng(10.008154123.635460);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(start,9));
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
buildGoogleAppClient();
mMap.setMyLocationEnabled(真);
}否则{
buildGoogleAppClient();
mMap.setMyLocationEnabled(真);
}
}
受保护的同步无效BuildGoogleAppClient(){
if(mGoogleApiClient==null){
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
if(mGoogleApiClient!=null){
mGoogleApiClient.connect();
}
}
它只是这样显示的 而不是


请不要介意标记。

我已使用fusedlocation提供程序API获取用户的当前位置

 public class activityPlot extends AppCompatActivity implements
            OnMapReadyCallback{

        private String TAG = activityPlot.class.getSimpleName();

        private GoogleMap mMap;
        private Boolean mLocationPermissionGranted = false;
        private FusedLocationProviderClient mFusedLocationProviderClient;
        private LatLng mDefaultLocation;
        private Location mLastKnownLocation;

        //  Request Code for Location Permission...
        private static final int LOCATION_PERMISSION_REQUEST_CODE = 438;

        @SuppressLint({"ClickableViewAccessibility", "ResourceType"})
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_plot);

            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            if (mapFragment != null) {
                View mapView = mapFragment.getView();
                if (mapView != null &&
                        mapView.findViewById(1) != null) {
                    @SuppressLint("ResourceType") View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                            locationButton.getLayoutParams();
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
                    layoutParams.setMargins(0, 0, 30, 200);
                }
                mapFragment.getMapAsync(this);
            }

            mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        }

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

            updateLocationUI();
            getDeviceLocation();
        }

        private void getLocationPermission() {
            Log.i(TAG, "asking for permission");
            if (ContextCompat.checkSelfPermission(this.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                mLocationPermissionGranted = true;
                updateLocationUI();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
            mLocationPermissionGranted = false;
            switch (requestCode) {
                case LOCATION_PERMISSION_REQUEST_CODE: {
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        mLocationPermissionGranted = true;
                    }
                }
            }
            updateLocationUI();
        }

        private void updateLocationUI() {
            if (mMap == null) {
                return;
            }
            try {
                if (mLocationPermissionGranted) {
                    mMap.setMyLocationEnabled(true);
                    mMap.getUiSettings().setMyLocationButtonEnabled(true);
                } else {
                    mMap.setMyLocationEnabled(false);
                    mMap.getUiSettings().setMyLocationButtonEnabled(false);
                    mLastKnownLocation = null;
                    getLocationPermission();
                }
            } catch (SecurityException e) {
                Log.e("Exception: %s", e.getMessage());
            }
        }

        private void getDeviceLocation() {
            try {
                if (mLocationPermissionGranted) {
                    Task locationResult = mFusedLocationProviderClient.getLastLocation();
                    locationResult.addOnCompleteListener(this, task -> {
                        if (task.isSuccessful()) {
                            mLastKnownLocation = (Location) task.getResult();
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), 17));
                        } else {
                            Log.d(TAG, "Current location is null. Using defaults.");
                            Log.e(TAG, "Exception: %s", task.getException());
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, 17));
                            mMap.getUiSettings().setMyLocationButtonEnabled(false);
                        }
                    });
                }
            } catch (SecurityException e) {
                Log.e("Exception: %s", e.getMessage());
            }
        }
    }
我的XML文件如下所示:->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    orientation="vertical"
    tools:context=".activityPlot">

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

</LinearLayout>

这段代码对我来说运行得很好。希望它在您的设备上也能正常工作。如果这不起作用,那么,1。尝试更新设备上的播放服务,2。尝试使用clean project和Rebuild project重新安装应用程序。
 //  Maps, Places and Location Libraries...
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-location:15.0.1'