Android realizes the custom pop-up function of Baidu map

When we use Baidu map, clicking the marker on the map will pop up a window for the detailed information of the location, as shown in the left figure below. Sometimes, we want to define the content of the pop-up window ourselves, or simply use our own data to construct such a pop-up window. However, in the latest Android SDK of Baidu map, There is no convenient class to operate this pop-up window. Although there is a popupoverlay, it only supports converting pop-up content into no more than three bitmaps. If there are buttons in this pop-up window to respond to click events, this can not meet the requirements. Therefore, after reading the API of Baidu map overlay, I decided to use the method of custom view to achieve similar effects, First paste the general effect picture, as shown on the right:

The basic principle is to use itemizedoverlay to add attachments. In the OnTap method, add a user-defined view to the MapView (if it already exists, it will be directly set as visible). Here is my implementation method:

1、 Custom overlay class: mypopupoverlay. This class is the most critical class, itemizedoverlay, which is used to set the marker and define the click event and pop-up window of the marker. As for the content of the pop-up window, it is constructed by defining the listener and putting it into the activity. If there are no special requirements, this class does not need to be changed. The code is as follows. The popplinear object is the custom view added to the map:

2、 Mainactivity, the main interface, is used to display maps and create mypopupoverlay objects. When using the mypopupoverlay class I wrote, you need to follow the following steps:

Create mypopupoverlay object. The constructor is public mypopupoverlay (context context, MapView, mmapview). The four parameters are the current context, the general marker (which is required by itemizedoverlay and the default marker when marker is not set) and Baidu map object. Set the layout file ID of the customized pop-up window content. The method used is public void setlayouteid (int layouteid). Set whether to use a custom marker or a pre written original marker of Baidu map with A-J words. The method used is public void setusedefaultmarker (Boolean usedefaultmarker). The original marker can be used only when this value is true and the setmarker method of overlayitem is not called to set the marker for a specific point. Create the point where Marker is located, that is, create OverlayItem one by one, then call public void addItem (OverlayItem item) or public void addItem (List<OverlayItem> List<OverlayItem>) method to add these to the custom additional layer. Add an OnTap event for mypopupoverlay object. When the marker is clicked, fill the contents in the pop-up window (that is, the contents in the layoutid layout in Article 2). The setting method is public void setontaplistener (ontaplistener ontaplistener). Ontaplistener is an interface defined in mypopupoverlay. To implement this interface, you need to override public void OnTap (int index, view, popupview) method, where index represents the index of the clicked marker (to be exact, overlayitem). Popview is the view using the layouteid layout, that is, the part of the pop-up window except the small triangle below. Add this mypopupoverlay object to the map: mmapview. Getoverlays(). Add (myoverlay); mmapview. Refresh() ; here is my code (mainactivity):

This is the main idea and code, because there are many code files and resource files, it is not easy to post all the codes that can run directly, and there are too many controls in the layout file, which is not easy to understand. Just write it like this. If you have any better methods or good suggestions, you are welcome to discuss and correct them.

Note: in order to illustrate the problem, I simplified a lot of things in the main class, and some pictures are very troublesome to find. Attach the source code here for your reference. Before running, you need to modify the key of Baidu map in mainactivity.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>