Study Notes on Android: Baidu map (search for bus transfer routes and transitoverlay)

Source: Internet
Author: User


Bus Transfer Route Search and transitoverlay

Here, only Java code is provided. For XML code, refer to: learning notes for Android and learning Baidu Map

Important code:

Mkplannode start = new mkplannode (); // start point: Tiananmen start.pt = new geopoint (INT) (40.003834809598516*1e6), (INT) (116.3263213634491*1e6 )); // set the central mapcontroller of the map. setcenter (start.pt); mkplannode end = new mkplannode (); // end point: nest end.pt = new geopoint (INT) (39.99142*1e6), (INT) (116.39026999999998*1e6); // sets the driving route search policy. The time is prioritized, the minimum fee is required, or the distance is the shortest./** ebus_time_first * Public static final int ebus_time_first * Public Transport retrieval policy constant: time First * ebus_transfer_first * Public static final int ebus_transfer_first * Public Transport retrieval policy constant: minimum transfer * ebus_assist_first * Public static final int ebus_assist_first * Public Transport retrieval policy constant: minimum walking distance * ebus_no_subway * Public static final int ebus_no_subway * Public Transit retrieval policy constant: Excluding Metro * // you can specify a travel route search policy, time First, minimum transfer, minimum walking distance, or no subway mksearch. settransitpolicy (mksearch. ebus_transfer_first); mksearch. transitsearch ("Beijing", start, end); // The city name must be set.

Implement ongettransitrouteresult (mktransitrouteresult, INT) of mysearchlistener and display the search result:

@ Overridepublic void ongettransitrouteresult (mktransitrouteresult result, int ierror) {If (result = NULL) {return;} transitoverlay = new transitoverlay (mymapactivity. this, mmapview); // only one scheme is shown here as an example of transitoverlay. setdata (result. getplan (0); mmapview. getoverlays (). add (transitoverlay );}

API:

Transitsearch

Public intTransitsearch(Java. Lang. String city, mkplannode start, mkplannode end)

Search bus routes.

Asynchronous function, which returns the ongettransitrouteresult method notification in the mksearchlistener.
Parameters:

City-city name, used for retrieval in which city
Start-the start point of the search. You can specify the start point by using the keyword or coordinate.
End-the end point of the search. You can specify the end point by using the keyword or coordinate.
Return Value:

0 is returned for success; otherwise-1 is returned.
Settransitpolicy

Public intSettransitpolicy(INT Policy)

Set route planning policies.

The parameter is a policy constant. Valid for next search
Parameters:

Policy-ebus_time_first: time first; ebus_transfer_first: less transfer; ebus_assist_first: less walk; ebus_no_subway: Non-Subway
Return Value:

0 is returned for success; otherwise-1 is returned.

Specific implementation:

Package xiaosi. baidumap; import android. app. alertdialog; import android. content. dialoginterface; import android. OS. bundle; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. geopoint; import COM. baidu. mapapi. mkaddrinfo; import COM. baidu. mapapi. mkdrivingrouteresult; import COM. baidu. mapapi. mkplannode; import COM. baidu. mapapi. mkpoiinfo; import COM. baidu. mapapi. mkpoiresult; import COM. baidu. mapapi. mkse Arch; import COM. baidu. mapapi. mksearchlistener; import COM. baidu. mapapi. mktransitrouteresult; import COM. baidu. mapapi. mkwalkingrouteresult; import COM. baidu. mapapi. mapactivity; import COM. baidu. mapapi. mapcontroller; import COM. baidu. mapapi. mapview; import COM. baidu. mapapi. poioverlay; import COM. baidu. mapapi. routeoverlay; import COM. baidu. mapapi. transitoverlay; public class baidumapactivity extends mapactivi Ty {/** called when the activity is first created. */private bmapmanager mapmanager = NULL; private string key = "Hangzhou"; private mapview = NULL; private mksearch; private mapcontroller = NULL; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mapmanager = new bmapman Ager (getapplication (); mapmanager. init (Key, null); super. initmapactivity (mapmanager); mapview = (mapview) findviewbyid (R. id. mapview); // enables the built-in zoom control mapview. setbuiltinzoomcontrols (true); // get control of mmapview, which can be used to control and drive translation and zoom mapcontroller = mapview. getcontroller (); // sets the map zoom level mapcontroller. setzoom (12); mksearch = new mksearch (); // note that only one mksearchlistener is supported. The last setting prevails over mksearch. init (mapmanager, new mysearchlisten Er (); mkplannode start = new mkplannode (); // start point: Tiananmen start.pt = new geopoint (INT) (40.003834809598516*1e6), (INT) (116.3263213634491*1e6); // sets the map's central mapcontroller. setcenter (start.pt); mkplannode end = new mkplannode (); // end point: nest end.pt = new geopoint (INT) (39.99142*1e6), (INT) (116.39026999999998*1e6); // sets the driving route search policy, with time first, minimum cost, or minimum distance./** ebus_time_first * Public static final int ebus_time_first * Public Transport Retrieval policy constant: Time First * ebus_transfer_first * Public static final int ebus_transfer_first * Public Transport retrieval policy constant: minimum transfer * ebus_assist_first * Public static final int ebus_assist_first * Public Transport retrieval policy constant: minimum walking distance * ebus_no_subway * Public static final int ebus_no_subway * Public Transit retrieval policy constant: Excluding Metro * // you can specify a travel route search policy, time First, minimum transfer, minimum walking distance, or no subway mksearch. settransitpolicy (mksearch. ebus_transfer_first); mksearch. transitsearch ("Beijing", start, end); // you must set the city name} public Class mysearchlistener implements mksearchlistener {public void ongetaddrresult (mkaddrinfo arg0, int arg1) {/*** return the address information search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that the search result is correct and the result contains related results. 100 indicates that the search result is correct, no relevant address information */} public void ongetdrivingrouteresult (mkdrivingrouteresult arg0, int arg1) {/** returns the driving route search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that the */} public void ongetpoiresult (mkpoiresult arg0, int arg1, int arg2) is returned. {/** the poi search result is returned. Parameter: arg0-search result arg1-return result type: mksearch. type_poi_list mksearch. type_area_poi_list mksearch. type_city_list arg2-error number. 0 indicates that the public void ongettransitrouteresult (mktransitrouteresult arg0, int arg1) is returned. Parameter: arg0-arg1-error number of the search result. 0 indicates that a correct result is returned. When mkevent is returned. error_route_addr indicates that the start or end point is ambiguous. Call the getaddrresult method of mktransitrouteresult to obtain the recommended start or end point information */If (arg0 = NULL) {return ;} transitoverlay = new transitoverlay (baidumapactivity. this, mapview); // only one scheme is shown here as an example of transitoverlay. setdata (arg0.getplan (0); mapview. getoverlays (). add (transitoverlay);} public void ongetwalkingrouteresult (mkwalkingrouteresu Lt arg0, int arg1) {/** return the walking route search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that the returned result is correct */}}@ overrideprotected Boolean isroutedisplayed () {return false;} @ overrideprotected void ondestroy () {If (mapmanager! = NULL) {mapmanager. Destroy (); mapmanager = NULL;} super. ondestroy () ;}@ overrideprotected void onpause () {If (mapmanager! = NULL) {mapmanager. Stop () ;}super. onpause () ;}@ overrideprotected void onresume () {If (mapmanager! = NULL) {mapmanager. Start () ;}super. onresume ();}}

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.