Android studio Gaode map development

First look at the official website

Link: Gaode official website .

1. The first step is to configure AndroidManifest.xml

<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />     
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

Then, set Gaode Key

Link: Get the key from the official website console .
Add the following content to the application tag:

<meta-data android:name="com.amap.api.v2.apikey" android:value="key">
//开发者申请的key  
</meta-data>

2. The second step is to add the map development package to the project

insert image description here

  1. With default configuration, no need to modify build.gradle. Create a folder jniLibs in the main directory (if there is no need to create it), copy the armeabi folder of the downloaded file to the jniLibs directory. If this directory already exists, copy the downloaded so library to this directory.
    2 . Put the jar package into the libs directory. For each jar file, right click - select Add As Library and import it into the project. Or use the menu bar to select File ->Project Structure->Modules-> Dependencies. Click the green plus sign to select File dependency. Then select the jar package to add

3. The third step is to initialize the map container

  • xml
<com.amap.api.maps.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
  • Java
public class MainActivity extends Activity {
    
    
   private MapView mMapView = null;
    //初始化地图控制器对象
    AMap aMap;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);
    //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        button = (Button) findViewById(R.id.button);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
    
    
            aMap = mMapView.getMap();
        }
         //获取地图
        initmap();
  }

 private void initmap() {
    
    
        //加一个,再次执行
        AMapLocationClient.setApiKey("你的key值")

        LatLng latLng = new LatLng(27.987247,119.804769);//经纬度

        aMap.getUiSettings().setLogoBottomMargin(-50);//logo移出可视界面
        aMap.getUiSettings().setZoomGesturesEnabled(false);//禁用双击地图可以使缩放级别增加1 (放大)
        aMap.getUiSettings().setZoomControlsEnabled(false);//禁用加减号控制缩放 管理缩放控件 + -
        aMap.getUiSettings().setRotateGesturesEnabled(false);//禁用旋转手势
        aMap.getUiSettings().setTiltGesturesEnabled(false);//禁用倾斜手势
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng)); //设置中心点
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15f)); //设置缩放比例 f
        //设置自定义地图样式
        aMap.setCustomMapStyle(
                new CustomMapStyleOptions()
                        .setEnable(true)
                        .setStyleData(GetAssetsStyle.getInstance().getAssetsStyle(MainActivity.this,"style.data"))
                        .setStyleExtraData(GetAssetsStyle.getInstance().getAssetsStyle(MainActivity.this,"style_extra.data"))
        );
        //绘制默认 Marker -- Marker 常用属性、 position  在地图上标记位置的经纬度值。必填参数、title 点标记的标题e、snippet   点标记的内容
        // draggable 点标记是否可拖拽、 visible 点标记是否可见、anch 点标记的锚点、alpha 点的透明度
        final Marker marker = aMap.addMarker(new MarkerOptions()
                .position(latLng)//在地图上标记位置的经纬度值
                .title("预警")//点标记的标题
                .snippet("嘀嘀嘀"));//点标记的内容
        marker.showInfoWindow();//显示自定义marker
      }

  @Override
  protected void onDestroy() {
    
    
    super.onDestroy();
    //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
    mMapView.onDestroy();
  }
 @Override
 protected void onResume() {
    
    
    super.onResume();
    //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
    mMapView.onResume();
    }
     @Override
 protected void onPause() {
    
    
    super.onPause();
    //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
    mMapView.onPause();
    }
 @Override
 protected void onSaveInstanceState(Bundle outState) {
    
    
    super.onSaveInstanceState(outState);
    //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
    mMapView.onSaveInstanceState(outState);
  } 
}

4. Customize the map

insert image description here

  • Download offline style.data style_extra.data
  • Put it in the static file assets
 aMap.setCustomMapStyle(
                new CustomMapStyleOptions()
                        .setEnable(true)
                        .setStyleData(GetAssetsStyle.getInstance().getAssetsStyle(MainActivity.this,"style.data"))
                        .setStyleExtraData(GetAssetsStyle.getInstance().getAssetsStyle(MainActivity.this,"style_extra.data"))
        );

Guess you like

Origin blog.csdn.net/afufufufu/article/details/119414491#comments_27354837