Android Baidu Map Development Example (reprint)

Source: Internet
Author: User

Because in my bedroom Google Base station location returned data is always empty, so instead of Baidu map, found that Baidu map development is very convenient, provide a lot of useful tools, map loading speed is much faster than Google Maps.

In order to strengthen memory, write a little Android Baidu map development commonly used methods.

1 initialization

Mapmanager Mbmapman = new Bmapmanager (this);
Boolean issuccess = Mbmapman.init (This.mstrkey, New Mygenerallistener ());

Issuccess value is true, the map initialization succeeds

2 Positioning | Setting up a location listener

Mbmapman.getlocationmanager (). Setnotifyinternal (10, 5);
Mbmapman.start ();

Mlocationlistener = new Locationlistener () {

public void onlocationchanged (location location)
{
TODO auto-generated Method Stub
TODO auto-generated Method Stub
if (location = null)
{
String Strlog = String.Format ("Your current location: \ r \ n" +
"Latitude:%f\r\n" +
"Longitude:%f",
Location.getlatitude (), Location.getlongitude ());
LOG.V ("Application-strlog", Strlog);
Mbmapman.getlocationmanager (). Removeupdates (Mlocationlistener);
}
}



};
Mbmapman.getlocationmanager (). Requestlocationupdates (Mlocationlistener);

3 usage Examples

Duduapp app = (Duduapp) this.getapplication ();
If you are using the Map SDK, initialize your map activity
Super.initmapactivity (App.mbmapman);

Mmapview = (Mapview) Findviewbyid (R.ID.MAP);
Mmapview.setbuiltinzoomcontrols (TRUE); Setting enable built-in zoom controls

Mmapcontroller = Mmapview.getcontroller (); Get control of Mmapview and use it to control and drive panning and zooming
GeoPoint point = new GeoPoint (LATITUDEE6,LONGITUDEE6); Constructs a geopoint with a given latitude and longitude, in the Micro degree (degree * 1E6)
Mmapcontroller.setcenter (point); Set Map Center points

Mmapcontroller.setzoom (Zoomlevel); Set the map zoom level
Mmapview.setdrawoverlaywhenzooming (TRUE);
drawable marker = getresources (). getdrawable (R.drawable.iconmarka); Get the resources that need to be labeled on the map
Marker.setbounds (0, 0, marker.getintrinsicwidth (), marker
. Getintrinsicheight ()); Defining locations and boundaries for maker

OVERITEMT Overitem = new Overitemt (marker,1);
Mmapview.getoverlays (). Add (Overitem); Add Itemizedo

4 covering material

Class Overitemt extends Itemizedoverlay<overlayitem> {


Public list<overlayitem> mgeolist = new arraylist<overlayitem> ();
Private drawable marker;





Public Overitemt (drawable marker,int count) {
Super (Boundcenterbottom (marker));


This.marker = marker;


Constructs a geopoint with a given latitude and longitude, measured in degrees of micro (degree * 1E6)
GeoPoint P1 = new GeoPoint (latitudeE6, longitudeE6);
GeoPoint P2 = new GeoPoint ((int) (Dudubll.getinstance (Getapplicationcontext ()). Model.blatitude*1e6),
(int) (Dudubll.getinstance (Getapplicationcontext ()). model.blongitude*1e6));

The three parameters for constructing Overlayitem are: item position, title text, text fragment
Mgeolist.add (New Overlayitem (P1, "P1", "store Location"));
Mgeolist.add (New Overlayitem (P2, "P2", "my Position"));

Populate (); The CreateItem (int) method constructs the item. Once you have the data, call this method first before calling other methods
}


public void UpdateOverlay ()
{
Populate ();
}


@Override
public void Draw (canvas canvas, Mapview Mapview, Boolean shadow) {


The projection interface is used for transformations between screen pixel coordinates and latitude and longitude coordinates
Projection Projection = Mapview.getprojection ();
Point P1=null;
Point P2=null;
for (int index = size ()-1; index >= 0; index--)
{//Traverse mgeolist
Overlayitem Overlayitem = GetItem (index); Gets the item for the given index

String title = Overlayitem.gettitle ();

LOG.V ("Map_title", title);
Paint Painttext = new paint ();
Painttext.setcolor (Color.Blue);
Painttext.settextsize (15);
if (Title.equals ("P1"))
{
Shifts the latitude to the screen pixel coordinates relative to the upper-left corner of the Mapview
P1= Projection.topixels (Overlayitem.getpoint (), NULL);

You can add your drawing code here


Canvas.drawtext ("Shops", p1.x-30, p1.y-20, Painttext); Draw text
}
Else
{
P2= Projection.topixels (Overlayitem.getpoint (), NULL);
Canvas.drawtext ("My position", p2.x-30, p2.y-20, Painttext); Draw text
}
}

Super.draw (canvas, Mapview, shadow);
Adjusts a drawable boundary so that (0,0) is a pixel at the center of the last line at the bottom of the drawable
Boundcenterbottom (marker);
}


@Override
protected Overlayitem createItem (int i) {
TODO auto-generated Method Stub
return Mgeolist.get (i);
}


@Override
public int size () {
TODO auto-generated Method Stub
return Mgeolist.size ();
}
@Override
Handling when clicking an event
protected Boolean onTap (int i) {
SetFocus (Mgeolist.get (i));

return true;
}


@Override
public boolean OnTap (GeoPoint arg0, Mapview arg1) {
TODO auto-generated Method Stub
Eliminate pop Bubbles

Return Super.ontap (arg0, arg1);
}
}

}

5 Route Enquiry

Mksearch msearch=new Mksearch ();

Msearch.init (app.mbmapman,new Mksearchlistener () {


public void Ongetaddrresult (mkaddrinfo arg0, int arg1) {
TODO auto-generated Method Stub

}


public void Ongetbusdetailresult (Mkbuslineresult arg0,
int arg1) {
TODO auto-generated Method Stub

}


public void Ongetdrivingrouteresult (Mkdrivingrouteresult res,int error) {
TODO auto-generated Method Stub
TODO auto-generated Method Stub
The error number can refer to the definition in mkevent
if (Error! = 0 | | res = = NULL) {
Toast.maketext (Mapactivity.this, "Sorry, no results found", Toast.length_short). Show ();
Return
}
Get route distance
int distance = Res.getplan (0). Getroute (0). Getdistance ();
Routeoverlay routeoverlay = new Routeoverlay (mapactivity.this, Mmapview);
Only one scenario is shown here as an example
Routeoverlay.setdata (Res.getplan (0). Getroute (0));
Mmapview.getoverlays (). Clear ();
Mmapview.getoverlays (). Add (Routeoverlay);
Mmapview.invalidate ();
Mmapview.getcontroller (). Animateto (Res.getstart (). PT);
}


public void Ongetpoiresult (Mkpoiresult arg0, int arg1,
int arg2) {
TODO auto-generated Method Stub

}


public void Ongetrgcshareurlresult (String arg0, int arg1) {
TODO auto-generated Method Stub

}


public void Ongetsuggestionresult (Mksuggestionresult arg0,
int arg1) {
TODO auto-generated Method Stub

}


public void Ongettransitrouteresult (
Mktransitrouteresult arg0, int arg1) {
TODO auto-generated Method Stub

}


public void Ongetwalkingrouteresult (
Mkwalkingrouteresult res, int error) {
if (Error! = 0 | | res = = NULL) {
Toast.maketext (Mapactivity.this, "Sorry, no results found", Toast.length_short). Show ();
Return
}
Get route distance
int distance = Res.getplan (0). Getroute (0). Getdistance ();
Routeoverlay routeoverlay = new Routeoverlay (mapactivity.this, Mmapview);
Only one scenario is shown here as an example
Routeoverlay.setdata (Res.getplan (0). Getroute (0));
Mmapview.getoverlays (). Clear ();
Mmapview.getoverlays (). Add (Routeoverlay);
Mmapview.invalidate ();
Mmapview.getcontroller (). Animateto (Res.getstart (). PT);

}

});

Walking route

Msearch.walkingsearch (NULL, startnode, NULL, Endnode);

Please refer to http://developer.baidu.com/map/sdkandev-question.htm for details.

6google map and Baidu Map coordinate system to replace

The conversion method uses the Baidu API.
The conversion method is:
http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=120.0904441&y=30.3056719
The type1 and type2 can be converted by filling in the corresponding values.
Type=0 of GPS coordinates
type=2 of Google coordinates
The type=4 of Baidu coordinates
The resulting form is:
{"Error": 0, "x": "mte2ljm1mta3odgzmdm=", "Y": "Mzkuotgwotiwndewmtu2″}x and Y are all base64 converted.

Baidu Map Development is very convenient, interested can go to see the API

Http://developer.baidu.com/map/sdk-android.htm

Android Baidu Map Development Example (reprint)

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.