Android develops Gaode map custom map application [latest]

Call the Gaode map SDK on the app, and want to change the map style to a custom style. The development manual is not very clear, here are the detailed steps:

main point

  1. The map file is placed in the virtual machine path
  2. Relevant permissions open

(1) Download and install the Gaode map SDK, interface configuration, there are many introductions on the Internet. No more details here.
(2) Customize the map to create a map style
(3) Publish and use
insert image description here
After creating a map style, you first need to publish it, and then click Use and Share. Choose Android, the latest SDK I downloaded here is version 7.0.0 or later
insert image description here
(4) [Key]
After the application in Android is downloaded and decompressed, you will get two .data files. It is said in the development document that
insert image description here
the path here refers to the path in the virtual machine! Not on the local computer! !
I use the Thunderbolt simulator, open the file manager, and directly drag in two .data files. I don’t know why they are automatically placed in insert image description here
the path. I am too lazy to change it.

Go back to Android studio and add in the corresponding .java file

        //地图样式
        aMap.setCustomMapStyle(
                new com.amap.api.maps.model.CustomMapStyleOptions()
                        .setEnable(true)
                        .setStyleDataPath("/storage/emulated/0/Pictures/style.data")
                        .setStyleExtraPath("/storage/emulated/0/Pictures/style_extra.data")
        );

Re-run the application, and you may find that the map style is still the default style, no change!
insert image description here

To open the software to read storage permissions! ! () Long press the application icon on the main interface to enter the permission setting
Finally, re-run the application ~ success!
insert image description here
See below for complete code

public class TrailFragment extends Fragment{
    
    
    private MapView mMapView = null;
    private AMap aMap = null;

    public TrailFragment() {
    
    
        // Required empty public constructor
    }
    

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
    

        View view = inflater.inflate(R.layout.fragment_trail, container, false);
        //获取地图空间引用
        mMapView = (MapView) view.findViewById(R.id.map);
        // 在acitivity执行OnCreate时执行mMapView.onCreate(savedInstanceState);
        mMapView.onCreate(savedInstanceState);// 此方法必须重写
        
        if (aMap == null) {
    
    
            aMap = mMapView.getMap();
        }
        
        aMap.setCustomMapStyle(
                new com.amap.api.maps.model.CustomMapStyleOptions()
                        .setEnable(true)
                        .setStyleDataPath("/storage/emulated/0/Pictures/style.data")
                        .setStyleExtraPath("/storage/emulated/0/Pictures/style_extra.data")
        );
         return view;
}

    @Override
    public void onDestroy() {
    
    
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mMapView.onDestroy();
    }

    @Override
    public void onResume() {
    
    
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    public void onPause() {
    
    
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    
    
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
        }

Guess you like

Origin blog.csdn.net/weixin_43846562/article/details/112299444