百度地图API android studio中不显示地图只显示网格线的问题

一定要在当前project的包下面去申请API得到的AK才能用,还有ak是在manifest文件中标明的

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.baidumaptest">

    android:name="android.permission.ACCESS_NETWORK_STATE"/>
    android:name="android.permission.INTERNET"/>
    android:name="android.permission.ACCESS_WIFI_STATE"/>
    android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    android:name="android.permission.WRITE_SETTINGS"/>
    android:name="android.permission.READ_PHONE_STATE"/>
    android:name="android.permission.CALL_PHONE"/>
    android:name="android.permission.ACCESS_FINE_LOCATION"/>
    android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    android:name="ACCESS_MOCK_LOCATION"/>
    android:name="ACCESS_GPS"/>


            android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        android:name=".MainActivity">
            
                android:name="android.intent.action.MAIN"/>

                android:name="android.intent.category.LAUNCHER"/>
            
        
                    android:name="com.baidu.lbsapi.API_KEY"
            android:value="uUa7BnoXS8KGUEtbURnzGEfKRGidYo4k" />
    

MainActivity的代码如下:我的代码中不知道为什么MapController不能用,现在只是能显示地图,而无法以当前位置为中心显示

package com.example.baidumaptest;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.inner.GeoPoint;
import java.util.List;

public class MainActivity extends Activity {
    private BMapManager mBMapManager;
    private MapView mMapView;
    private LocationManager mLocationManager;
    private String provider;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBMapManager = new BMapManager();
        /**
         * SDKInitializer.initialize(getApplicationContext());
         * 这个方法初始化,key 也不是写在代码里,而是放到manifast里。
         */
        SDKInitializer.initialize(getApplicationContext());
        mBMapManager.getContext();
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.map_view);
        mMapView.showZoomControls(true);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        List providerList = mLocationManager.getProviders(true);
        if (providerList.contains(LocationManager.GPS_PROVIDER)) {
            provider = LocationManager.GPS_PROVIDER;
        } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;
        } else {
            Toast.makeText(MainActivity.this, "没有可以定位的位置提供器可用", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // 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 ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = mLocationManager.getLastKnownLocation(provider);
        if (location != null) {
            navigateTo(location);
        }
    }
        private void navigateTo(Location location){
        //MapController controller = mMapView.getController();
            //controller.setZoom(16);
        GeoPoint point=new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
        //mMapView.setCenter(point);
        }


    
    @Override
    protected void onResume(){
        mMapView.onResume();
        
        super.onResume();
    }
    @Override
    protected void onPause(){
        mMapView.onPause();
        
        super.onResume();
    }
    @Override
    protected void onDestroy(){
        mMapView.onDestroy();
        
        super.onDestroy();
    }
}








你可能感兴趣的:(百度地图API使用问题)