“This is the 26th day of my participation in the August More Text Challenge.

My last tweet focused on the use of offline maps, which we can cache to meet certain business needs. Next, I will introduce the interaction between baidu map and the map.

From these interactions we can customize our software to meet the needs of a wide variety of customers.

Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!

Write some thoughts of the programmer, hope it will help you.

Baidu Map development – Interactive function with map 06 (QQ.com)

Interaction with maps

According to baidu map official tutorial display, you can control the map display, gesture control, event interaction, method interaction, element capping sequence, etc.

We highlight a few of the more common features

1. Map control

For example, the compass is on by default and can be turned off, and the rest is similar.

UiSettings mUiSetting = mBaiduMap.getUiSettings(); / / open the compass mUiSetting. SetCompassEnabled (true); / / the map the location of the Logo mMapView. SetLogoPosition (LogoPosition. LogoPostionleftBottom); / / / / scale by setting the enable to true or false. Choose whether to display scale mMapView showScaleControl (enable); / / zoom button mMapView showZoomControls (enable);Copy the code

Some general attributes of the map can be controlled by mbaidumap.getuisettings (). This specific look at the official tutorial can, the introduction of the list is very clear.

2. Map gestures

/ / map panning mUiSetting. SetScrollGesturesEnabled (true); / / map zoom mUiSetting. SetZoomGesturesEnabled (true); / / map overlooking mUiSetting. SetOverlookingGesturesEnabled (true); / / map rotation mUiSetting. SetRotateGesturesEnabled (true); / / disable all gestures mUiSetting setAllGesturesEnabled (false); / / double-click the center of the map zoom mUiSetting. SetEnlargeCenterWithDoubleClickEnable (true);Copy the code

Both of the above can be displayed by setting the map corresponding to their state, which is easy to achieve.

3. Event monitoring mechanism for maps

Let’s take a look at the event mechanism of Baidu Map. The interface reserved for the event mechanism of Baidu map is very rich. Let me briefly introduce various types of monitoring events.

Key verification time monitor, broadcast monitor, map event monitor and so on are very rich, which can basically meet the needs of our daily software for map controls.

Here we focus on the map event monitoring, which is also used frequently in daily development.

SDK defines a variety of map-related event monitoring, providing corresponding event monitoring methods, including map state change, gesture events, map rendering, map screenshots and so on.

Map click event listener interface

/ / map mBaiduMap click event listeners. SetOnMapClickListener (new BaiduMap. OnMapClickListener () {/ * * * * * map click event callback / @ Override public Void onMapClick (LatLng LatLng) {Toast. MakeText (getApplicationContext (), "click the longitude and latitude location:" + LatLng. Latitude, Toast. LENGTH_SHORT);  } /* Override public void onMapPoiClick(MapPoi) {}};Copy the code
  • Map Marker overlay click event monitor interface
/ / click mBaiduMap mulch to monitor events. SetOnMarkerClickListener (new BaiduMap. OnMarkerClickListener () {/ * * * click on the map Marker covering events to monitor function * * / @override public Boolean onMarkerClick(Marker Marker) {// Perform some business logic processing, such as pop-up details, when clicking on the overlay. return false; }});Copy the code

Map screenshot callback interface. This is still more practical, many apps will let the implementation of screenshots function.

/ / screenshot function callback mBaiduMap. The snapshot (new BaiduMap. SnapshotReadyCallback () {@ Override public void onSnapshotReady (Bitmap Bitmap) {// Here you can get the Bitmap format to save the image, you just need to save it. if (null! Bitmap) = {saveBitmap (" map screenshots, bitmap, getApplicationContext ()); }}}); /** * Save Bitmap * * @param name file name * @param bm picture to save */ void saveBitmap(String name, Bitmap bm, String TargetPath = McOntext.getfilesdir () + "/images/"; // Check whether the specified folder path exists if (! fileIsExist(TargetPath)) { Log.d("Save Bitmap", "TargetPath isn't exist"); } else {File saveFile = new File(TargetPath, name);} else {File saveFile = new File(TargetPath, name); try { FileOutputStream saveImgOut = new FileOutputStream(saveFile); / / compress - compression mean bm.com press (Bitmap.Com pressFormat. JPEG, 80, saveImgOut); Saveimgout.flush (); saveImgOut.close(); Log.d("Save Bitmap", "The picture is save to your phone!" ); } catch (IOException ex) { ex.printStackTrace(); }}} Boolean fileIsExist(String fileName) {// Pass the specified path and check whether the path exists File File =new File(fileName); if (file.exists()) return true; Else {//file.mkdirs() return file.mkdirs(); }}Copy the code

This way to complete the map screenshots function, very convenient, but also very practical. Here is just a brief introduction to the implementation of the screenshots function, the official tutorial also has a variety of listening events, you can choose to implement according to their own business needs.

This part is not very difficult, mainly is the early stage you set up the whole framework, it is easy to achieve some details of the function points.

Today’s share is here, tomorrow will bring you to draw points, lines, circles and other functions on Baidu map operation.

A word of warning to everyone, see the DEMO, it is much better than the official documentation tutorial.

Small remarks

Life is short, I don’t want to go after what I can’t see, I just want to catch what I can see.

Original is not easy, give a attention.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.