Android Baidu map Positioning

Source: Internet
Author: User

Reference: http://blog.csdn.net/binyao02123202/article/details/7583244


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

Step 1:Obtain a key value for your project. Http://dev.baidu.com/wiki/static/imap/key/

My key is: 01be364003b456360dd1_8d178898f59ec8a20b

 

Step 2:First, copy the two files baidumapapi. jar and libbmapapiengine. So included in the API to the project root directory and the libs \ armeabi directory, and go to Project Properties> JAVA
Select "add jars" in build path-> libraries, select baidumapapi. jar, and return the result. In this way, you can use the API in your program.

 

 

Example 1: Display Baidu Map

 

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.baidu.mapapi.MapView        android:id="@+id/bmapView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:clickable="true" /></LinearLayout>

 

Package COM. baidu. mapapi. demo; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. mapactivity; import COM. baidu. mapapi. mapview; import android. OS. bundle; import android. util. log; public class mapviewdemo extends mapactivity {protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. mapviewdemo); bmapapidemoapp APP = (bmapapidemoapp) This. geta Pplication (); If (App. mbmapman = NULL) {app. mbmapman = new bmapmanager (getapplication (); app. mbmapman. init (App. mstrkey, new bmapapidemoapp. mygenerallistener ();} app. mbmapman. start (); // If map SDK is used, initialize map activitylong itime = system. nanotime (); super. initmapactivity (App. mbmapman); itime = system. nanotime ()-itime; log. D ("mapviewdemo", "The init time is" + itime); mapview = (mapview) findview Byid (R. id. bmapview); mapview. setbuiltinzoomcontrols (true); // mapview. setdoubleclickzooming (false); // mapview. settraffic (true) ;}@ overrideprotected void onpause () {bmapapidemoapp APP = (bmapapidemoapp) This. getapplication (); If (App. mbmapman! = NULL) app. mbmapman. stop (); super. onpause () ;}@ overrideprotected void onresume () {bmapapidemoapp APP = (bmapapidemoapp) This. getapplication (); app. mbmapman. start (); super. onresume () ;}@ overrideprotected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false;} // @ override // protected void ondestroy () {/// bmapapidemoapp APP = (bmapapidemoapp) This. getapplication (); // If (App. mbmapman! = NULL) {// app. mbmapman. Destroy (); // app. mbmapman = NULL; //}/super. ondestroy ();//}}

 

Example 2: Find the bus route

 

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "in"/> <edittext Android: Id = "@ + ID/City" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Beijing"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "city search"/> <edittext Android: Id = "@ + ID/searchkey" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "717"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/str_id_bus"/> <button Android: id = "@ + ID/search" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "@ string/str_id_start"/> </linearlayout> <COM. baidu. mapapi. mapview Android: Id = "@ + ID/bmapview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: clickable = "true"/> </linearlayout>

 

Package COM. baidu. mapapi. demo; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. toast; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. mkaddrinfo; import COM. baidu. mapapi. mkbuslineresult; import COM. baidu. mapapi. mkdrivingrouteresult; import COM. B Aidu. mapapi. mkpoiinfo; import COM. baidu. mapapi. mkpoiresult; import COM. baidu. mapapi. mksearch; import COM. baidu. mapapi. mksearchlistener; import COM. baidu. mapapi. mksuggestionresult; import COM. baidu. mapapi. mktransitrouteresult; import COM. baidu. mapapi. mkwalkingrouteresult; import COM. baidu. mapapi. mapactivity; import COM. baidu. mapapi. mapview; import COM. baidu. mapapi. routeoverlay; import COM. baidu. mapapi. demo. R; Public class buslinesearch extends mapactivity {button mbtnsearch = NULL; // Search button mapview mmapview = NULL; // map viewmksearch msearch = NULL; // search module, you can also remove the map module from using string mcityname = NULL; protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. buslinesearch); bmapapidemoapp APP = (bmapapidemoapp) This. getapplication (); If (App. mbmapman = NULL) {app. Mbmapman = new bmapmanager (getapplication (); app. mbmapman. init (App. mstrkey, new bmapapidemoapp. mygenerallistener ();} app. mbmapman. start (); // If map SDK is used, initialize map activity super. initmapactivity (App. mbmapman); mmapview = (mapview) findviewbyid (R. id. bmapview); mmapview. setbuiltinzoomcontrols (true); // set overlay to be displayed during the scaling animation. mmapview is not drawn by default. setdrawoverlaywhenzooming (true); // initialize the search module and register the event listener msearch = new mkse Arch (); msearch. init (App. mbmapman, new mksearchlistener () {public void ongetpoiresult (mkpoiresult res, int type, int error) {// refer to the definition of IF (error! = 0 | res = NULL) {toast. maketext (buslinesearch. this, "Sorry, no results found", toast. length_long ). show (); return;} // find the bus route poi node mkpoiinfo curpoi = NULL; int totalpoinum = res. getnumpois (); For (INT idx = 0; idx <totalpoinum; idx ++) {log. D ("busline", "The busline is" + idx); curpoi = res. getpoi (idx); If (2 = curpoi. epoitype) {break ;}} msearch. buslinesearch (mcityname, curpoi. UID);} public void ongetdri Vingrouteresult (FIG, int error) {} public void ongettransitrouteresult (mktransitrouteresult res, int error) {} public void ongetwalkingrouteresult (mkwalkingrouteresult res, int error) {} public void ongetaddrresult (mkaddrinfo res, int error) {} public void ongetbusdetailresult (mkbuslineresult result, int ierror) {If (ierror! = 0 | result = NULL) {toast. maketext (buslinesearch. this, "Sorry, no results found", toast. length_long ). show (); return;} routeoverlay = new routeoverlay (buslinesearch. this, mmapview); // only one scheme is shown here as an example of routeoverlay. setdata (result. getbusroute (); mmapview. getoverlays (). clear (); mmapview. getoverlays (). add (routeoverlay); mmapview. invalidate (); mmapview. getcontroller (). animateto (result. getbusroute (). getstart () ;}@ overridepublic void ongetsuggestionresult (mksuggestionresult res, int arg1) {// todo auto-generated method stub }}); // set the search button response mbtnsearch = (button) findviewbyid (R. id. search); onclicklistener clicklistener = new onclicklistener () {public void onclick (view v) {searchbuttonprocess (v) ;}}; mbtnsearch. setonclicklistener (clicklistener);} void searchbuttonprocess (view v) {If (mbtnsearch. equals (V) {edittext editcity = (edittext) findviewbyid (R. id. city); edittext editsearchkey = (edittext) findviewbyid (R. id. searchkey); mcityname = editcity. gettext (). tostring (); msearch. poisearchincity (mcityname, editsearchkey. gettext (). tostring () ;}@overrideprotected void onpause () {bmapapidemoapp APP = (bmapapidemoapp) This. getapplication (); app. mbmapman. stop (); super. onpause () ;}@ overrideprotected void onresume () {bmapapidemoapp APP = (bmapapidemoapp) This. getapplication (); app. mbmapman. start (); super. onresume () ;}@ overrideprotected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false ;}}

For: http://download.csdn.net/detail/niejing654092427/5052395

Related Article

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.