Android Baidu map depicts poi

Source: Internet
Author: User
Tags asin ontap

First, the style is consistent. Please take a look at some legends:


In the previous article, I introduced how to display my location on a map. In this article, I will briefly introduce how to show a series of POI on a map)

First, we pull information to mark poi from the server, such as (Lat, Lon, tag, and so on), and then create itemizedoverlay Based on the latitude and longitude. This is used to display a series of poi.

Code snippet:

Call in onlocationchanged:

/***** Location listener ** @ Param arg0 */@ overridepublic void onlocationchanged (location) {If (location! = NULL) {// obtain your own latitude and longitude point geopoint = new geopoint (INT) (location. getlatitude () * 1e6), (INT) (location. getlongpolling () * 1e6); mmapcontroller. setcenter (geopoint); additemizedoverlay (geopoint. getlatitudee6 ()/1e6, geopoint. getlongitudee6 ()/1e6); // locate a series of points // addoverlay (geopoint );}}

/***** Add a series of overlay ranges within 5000. ** @ Param lat * latitude * @ Param Lon * longitude */void additemizedoverlay (double Lat, double Lon) {arraylist = new arraylist <markinfo> (); // retrieves a series of vertices returned by the server. arraylist <markinfo> markinfos = constent. getarraylistpoint (); For (INT I = 0; I <markinfos. size (); I ++) {markinfo = markinfos. get (I); double Dis = myutil. getgeopointdistance (Lat, Lon, markinfo. lat, markinfo. lon); If (DIS <= distance) arraylist. add (markinfo);} drawable marker = getresources (). getdrawable (R. drawable. ITEM); marker. setbounds (0, 0, marker. getintrinsicwidth (), marker. getintrinsicheight (); // defines the location and boundary for the maker overlays = mmapview. getoverlays (); myitemizedoverlay = new myitemizedoverlay (Marker, this, arraylist); myitemizedoverlay. setmapview (mmapview); overlays. add (myitemizedoverlay); mpopview = createmarkview (); // obtain the bubble viewmyitemizedoverlay. setmpopview (mpopview); // Add the bubble to mmapview. addview (mpopview, new mapview. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content, null, mapview. layoutparams. wrap_content); mpopview. setvisibility (view. gone );}

/***** Pop-up bubble when you create a click mark */public view createmarkview () {layoutinflater = (layoutinflater) getsystemservice (context. layout_inflater_service); mpopview = layoutinflater. inflate (R. layout. popview, null); textview = (textview) mpopview. findviewbyid (R. id. SNIPPET); mpopview. settag (textview); Return mpopview ;}

Here I will simulate a series of POI on the server and calculate the distance between myself and these poi. If it is smaller than 5000, it will be displayed.

It is explained here: first, the processing of the server side cannot return all the poi data to us. If so, the mobile phone will consume a lot of time, non-compliant with the needs of mobile projects, the server side should handle this series of poi, for example, based on different regions, for example, I pass the server parameter to Shanghai Pudong, so I will only return a series of POI in Pudong, which solves this problem. In addition, the best way is to use this logic on the server side, we request the server parameter to bring the distance of 5000 to the front and return it to the poi that we want to show directly. This is the best design. I am a beginner, but I will simply record it. If there is anything wrong or a simpler method, please give me some instructions. Thank you for taking the lead.

Myitemizedoverlay. Java

Package COM. JJ. baidu; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. point; import android. graphics. drawable. drawable; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android Oid. view. viewgroup. layoutparams; import android. widget. textview; import android. widget. toast; import COM. baidu. mapapi. geopoint; import COM. baidu. mapapi. itemizedoverlay; import COM. baidu. mapapi. mapview; import COM. baidu. mapapi. overlayitem; import COM. baidu. mapapi. projection; import COM. JJ. modle. markinfo;/***** series covering ** @ author Zhangjia **/public class myitemizedoverlay extends itemizedoverlay <overlayitem> {Private list <overlayitem> overlayitems; private overlayitem, overlayitem2, overlayitem3, overlayitem4, overlayitem5, overlayitem6; private context mcontext; private drawable marker; // cover icon private view mpopview = NULL; // The bubble displayed when you click mark viewprivate mapview; private arraylist <markinfo> markinfos; // a series of latitude and longitude points // inject map mappublic void setmapview (mapview) {This. mapview = mapview;} // note Enter the bubble viewpublic void setmpopview (view mpopview) {This. mpopview = mpopview;} public myitemizedoverlay (drawable marker, context, arraylist <markinfo> markinfos) {super (boundcenterbottom (Marker); this. mcontext = context; this. marker = marker; this. markinfos = markinfos; overlayitems = new arraylist <overlayitem> (); For (INT I = 0; I <markinfos. size (); I ++) {/*** the first parameter is geopoint, and the second parameter is title. overlayi TEM. gettitle () can be obtained, * the third parameter is snippet, through overlayitem. getsnippet () obtained. */geopoint = new geopoint (INT) (markinfos. get (I ). lat * 1e6), (INT) (markinfos. get (I ). lon * 1e6); overlayitem = new overlayitem (geopoint, I + "", markinfos. get (I ). tag); overlayitems. add (overlayitem) ;}populate (); // createitem (INT) method to construct an item. Once data exists, call this method before calling other methods.} @ overridepublic void draw (canvas, mapview, Boolean shadow) {// super. draw (canvas, mapview, shadow); // The projection interface is used to transform between the pixel coordinates and the latitude and longitude coordinates of the screen. // some text can be drawn next to the point. // projection = mapview. getprojection (); // For (INT I = 0; I <overlayitems. size (); I ++) {// overlayitem = overlayitems. get (I); // String title = overlayitem. gettitle (); // converts the longitude and latitude to the screen pixel coordinate relative to the upper left corner of the mapview. // point = projection. topixels (overlayitem. getpoint (), null); // you can add your painting code here // paint painttext = new paint (); // painttext. setcolor (color. blue); // painttext. settextsize (15); // canvas. drawtext (title, point. x-30, point. y, painttext); // draw text //} super. draw (canvas, mapview, shadow); // adjust a drawable boundcenterbottom (Marker );} @ overrideprotected overlayitem createitem (INT position) {return overlayitems. get (position) ;}@ overridepublic int size () {return overlayitems. size () ;}// process the Click Event @ overrideprotected Boolean ONTAP (final int I) {// obtain the geopointgeopoint geopoint = overlayitems. get (I ). getpoint (); // obtain the width and height of the pin, int x = marker. getintrinsicheight (); int y = marker. getintrinsicwidth ();/***** the bubble parameter indicates the horizontal deviation of the view to be displayed. ** you can adjust it by yourself. The vertical value is displayed in the middle of the pin. -Y/2. */mapview. updateviewlayout (mpopview, new mapview. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content, geopoint,-3,-y/2, mapview. layoutparams. bottom_center); mpopview. setvisibility (view. visible); mapview. getcontroller (). animateto (geopoint); // move to the middle textview = (textview) mpopview. gettag (); textview. settext (overlayitems. get (I ). getsnippet (); mpopview. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {toast. maketext (mcontext, overlayitems. get (I ). getsnippet (), 1 ). show () ;}}); return Super. ONTAP (I );}}

The comments are already detailed. I will not mention them here.

Here, I will briefly explain how to display the mpopview by clicking the poi tag on the map.

mapView.updateViewLayout(mPopView, new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, geoPoint,-3, -y / 2, MapView.LayoutParams.BOTTOM_CENTER));

The fourth and fifth parameters are relative to (geopoint corresponds to the point on the screen. if it is not written, it indicates 0, 0, then the mpopview and poi point will overlap, which is unfriendly. You can test it to understand. so we need to make the mpopview display in the middle of the poi, so that the mpopview is moved up (the width of POI/2 ). I believe everyone understands this. (You can see the following running results)

Here I will show you how to calculate the distance and azimuth between two latitude and longitude points:

Public class myutil {// Private Static final double earth_radius = 6378137.0;/***** returns the distance between two geopoints */public static double getgeopointdistance (double lat_a, double lng_a, double lat_ B, double lng_ B) {double radlat1 = (lat_a * Math. (PI/180.0); double radlat2 = (lat_ B * Math. PI/180.0); Double A = radlat1-radlat2; Double B = (lng_a-lng_ B) * Math. PI/180.0; double S = 2 * Math. asin (math. SQRT (math. pow (math. sin (A/2), 2) + math. cos (radlat1) * Math. cos (radlat2) * Math. pow (math. sin (B/2), 2); s = S * earth_radius; S = math. round (S * 10000)/10000; return s;}/***** returns the azimuth angle between two geopoints */public static string getgeopointcorner (double lat_a, double lng_a, double lat_ B, double lng_ B) {double D = 0; lat_a = lat_a * Math. PI/180; lng_a = lng_a * Math. PI/180; lat_ B = lat_ B * Math. PI/180; lng_ B = lng_ B * Math. PI/180; D = math. sin (lat_a) * Math. sin (lat_ B) + math. cos (lat_a) * Math. cos (lat_ B) * Math. cos (lng_ B-lng_a); D = math. SQRT (1-D * D); D = math. cos (lat_ B) * Math. sin (lng_ B-lng_a)/d; D = math. asin (d) * 180/math. pi; // d = math. round (D * 10000); Return D + "";}}


Self-positioning (hey, do you have a brother together)


I have no contact with the data. You can make adjustments based on your project requirements,

In addition, you can click the position button in the lower left corner to locate your own location. The specific implementation is also very simple. We directly reference the locationoverlay class that encapsulates everything for us,

Mmapview. getcontroller (). animateto (locationoverlay. getmylocation.

If you want to move the arrow following your position:

/***** When the position changes, it will follow up */locationoverlay. runonfirstfix (New runnable () {@ overridepublic void run () {mmapview. getcontroller (). animateto (locationoverlay. getmylocation ());}});

There is no problem in the test, but it is not a little inferior to Baidu map, and the movement is not coherent. The reason is being studied. If you know something, please instruct one or two.

Let's talk about it first, and then add it later.


APP applications are generally these functions. They mainly show a series of POI. Simply put, they will be updated as soon as they are found.

If you have any questions, please leave a message, thanks for you.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.