Recently, you need to search nearby information on a map and click details to go to the details page, this feature has been done before, but it takes some time to do it after about a year and a half, so it is easy for you to view it later, and it is also convenient for friends who need the same function.
To add a markup layer on a map, we usually use the inherited overlay class to implement our own painting function. If you don't talk about it, paste the Code directly:
Public class markitemoverlayextendsoverlay {
// Save the data bundle object to be displayed on the Interface
Private bundle data;
// The latitude and longitude objects currently marked on the map
Private geopoint;
// Mark layer start X, Y axis coordinate, and end X, Y axis Coordinate
Private intstart_x = 0, start_y = 0, end_x = 0, end_y = 0;
// Mark layer click listener
Private onmarkitemclicklistener onclicklistener;
// Mark the Layer Height
Final int Height = 50;
// Offset on the X axis of the bottom arrow
Final int x_offset = 10;
// Offset on the Y axis of the bottom arrow
Final int y_offset = 20;
// Offset on the left X axis
Final int left_x_offset = 30;
Public markitemoverlay (geopoint point, bundle, onmarkitemclicklistener ){
This. geopoint = point;
This. Data = bundle;
This. onclicklistener = onmarkitemclicklistener;
}
@ Override
Public void draw (canvas, mapview, booleanisshape ){
Super. Draw (canvas, mapview, isshape );
// Identify whether to repeatedly draw a map when it is moved, and whether to paint a map when the map display area is smaller than a certain level
If (! Isshape & mapview. getzoomlevel ()> 13 ){
String locationname = data. getstring ("locationname ");
// Convert the latitude and longitude objects to screen coordinates through mokto projection
Point point = new point ();
Projection projection = mapview. getprojection ();
Projection. topixels (geopoint, point );
// Defines the paint brush object for painting text on the canvas.
Paint paint = new paint ();
Paint. setcolor (color. White );
Paint. settextsize (mapview. getzoomlevel () + 2 );
Paint. setstrokewidth (1 );
Paint. setstyle (style. fill_and_stroke );
Paint. settypeface (typeface. monospace );
// Specifies the paint object of the text background layer.
Paint text_layout_paint = new
Paint ();
Text_layout_paint.setcolor (0xff4a9ade );
// The X and Y coordinates of the current latitude and longitude projection on the map
Final int x = point. X;
Final inty = point. Y;
// X and Y coordinate values of the first point on the right of the line at the bottom of the tip
Int bottom_right_x = x + x_offset;
// Y axis in the lower right corner
Int bottom_right_y = y-y_offset;
// The width required for text display
Int width = locationname. Length () * (mapview. getzoomlevel () + 2) + left_x_offset;
// The X-axis coordinate in the lower right corner
Int right_point_x = bottom_right_x + width-2 * x_offset-left_x_offset;
// Y axis in the upper right corner
Int right_point_top_y = bottom_right_y-height;
// X axis in the upper left corner
Int left_point_x = right_point_x-width;
// Coordinates of the X axis in the upper left corner of the arrow
Int bottom_left_x = left_point_x + left_x_offset;
Start_x = left_point_x;
Start_y = right_point_top_y;
End_x = right_point_x;
End_y = bottom_right_y;
Path = New Path ();
// Move to the origin coordinate of the Start Painting
Path. moveTo (x, y );
// A straight line between the sharp angle and the bottom right corner
Path. lineto (bottom_right_x, bottom_right_y );
// The right line at the bottom
Path. lineto (right_point_x, bottom_right_y );
// Straight line of the Right Border
Path. lineto (right_point_x, right_point_top_y );
// Straight line of the upper border
Path. lineto (left_point_x, right_point_top_y );
// Straight line of the Left Border
Path. lineto (left_point_x, bottom_right_y );
// A straight line at the left half of the bottom
Path. lineto (bottom_left_x, bottom_right_y );
// A straight line from the sharp angle to the bottom left and bottom of the gap
Path. lineto (x, y );
// Draw a polygon to the canvas
Canvas. drawpath (path, text_layout_paint );
// Canvas. drawtextonpath (locationname, path, 10, 0, paint );
// Text information to be displayed for painting
Canvas. drawtext (locationname, left_point_x + 10, bottom_right_y-15, paint );
}
}
@ Override
Public Boolean
ONTAP (geopoint p_geopoint, mapview ){
Point point = new point ();
Projection projection = mapview. getprojection ();
Projection. topixels (p_geopoint, point );
Int x = point. X;
Int y = point. Y;
// Determine whether the Coordinate Position of the current click is within the range of the marked layer. If yes, the system notifies the outside to perform corresponding operations.
If (x> = start_x & x <= end_x) & (Y> = start_y & Y <= end_y )){
// System. Out. println ("ONTAP ");
If (onclicklistener! = NULL ){
Onclicklistener. onitemclick (data );
}
}
Return super. ONTAP (p_geopoint, mapview );
}
}
Custom interface object
Public interface onmarkitemclicklistener {
Public void onitemclick (bundle );
}
Add a layer in mapactivity and monitor whether the layer is clicked:
Bundle DATA =
New bundle ();
Data. putstring ("locationname", info. Name );
Data. putstring ("locationaddress", info. Address );
Message message = myhandler. obtainmessage (2, info.pt );
Message. setdata (data );
Message. sendtotarget ();
Use bundle and handle to pass data and Add Tag Layers
In the Custom Handler subclass, add a layer to the map:
Markitemoverlay overlay =
New markitemoverlay (geopoint) msg. OBJ, MSG. getdata (), onmarkitemclicklistener );
Mvsearch. getoverlays (). Add (overlay );
// Call the invalidate method in the UI thread to refresh the interface
Mvsearch. invalidate ();
Listener for layer click listening:
Onmarkitemclicklistener = newonmarkitemclicklistener (){
@ Override
Public void onitemclick (bundle ){
New alertdialog. Builder (main. This)
. Settitle (bundle. getstring ("locationname") + "Address ")
. Setmessage (bundle. getstring ("locationaddress "))
. Setpositivebutton ("OK", new dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int arg1 ){
Dialog. Dismiss ();
}
})
. Show ();
}
};
The code is very simple and can be simply modified based on your needs. The implementation is as follows:
Click the above "7-day hotel chain stores" to bring up a dialog box:
Hope to help friends who have such needs. The interface is very dry, and the functions are not perfect. You need to improve it yourself.