Android SDK implementation path planning via latitude

Source: Internet
Author: User
Let's take a look at the effect chart:
1, just enter the time:, the default for Beijing
2, click the "Bus" button:
3, click the "Car" button:
4, click the "Walk" button
All right, let's get this done!
layout : <?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"
android:orientation= "Vertical" >
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<button
Android:id= "@+id/btn_bus"
Android:layout_width= "0DP"
android:layout_height= "Wrap_content"
Android:layout_weight= "1″
android:onclick= "DoClick"
android:text= "Bus"/>
<button
Android:id= "@+id/btn_car"
Android:layout_width= "0DP"
android:layout_height= "Wrap_content"
Android:layout_weight= "1″
android:onclick= "DoClick"
android:text= "Car"/>
<button
Android:id= "@+id/btn_walk"
Android:layout_width= "0DP"
android:layout_height= "Wrap_content"
Android:layout_weight= "1″
android:onclick= "DoClick"
android:text= "Walk"/>
</LinearLayout>
<com.amap.api.maps.mapview
Android:id= "@+id/map"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" ></com.amap.api.maps.MapView>
</LinearLayout>
Function:
Package cn.zmit.xianneng.activity;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Com.amap.api.maps.AMap;
Import Com.amap.api.maps.MapView;
Import Com.amap.api.maps.overlay.BusRouteOverlay;
Import Com.amap.api.maps.overlay.DrivingRouteOverlay;
Import Com.amap.api.maps.overlay.WalkRouteOverlay;
Import Com.amap.api.services.core.LatLonPoint;
Import Com.amap.api.services.route.BusPath;
Import Com.amap.api.services.route.BusRouteResult;
Import Com.amap.api.services.route.DrivePath;
Import Com.amap.api.services.route.DriveRouteResult;
Import Com.amap.api.services.route.RouteSearch;
Import Com.amap.api.services.route.WalkPath;
Import Com.amap.api.services.route.WalkRouteResult;
Import CN.ZMIT.XIANNENG.R;
/**
* Created by Kyle_zheng on 2016/1/20 0020.
*/
public class Testactivity extends activity implements Routesearch.onroutesearchlistener {
Mapview Mapview;
AMap AMap;
Latitude and longitude of routesearch.fromandto fromandto;//starting point and end-point
Routesearch Routesearch;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.test);
Mapview = (Mapview) Findviewbyid (R.ID.MAP);
Mapview.oncreate (savedinstancestate);
if (AMap = = null) {
AMap = Mapview.getmap ();
}
Latlonpoint start = new Latlonpoint (31.286389, 118.378039);//starting point for the latitude and longitude of the new campus of Ann Normal University
Latlonpoint end = new Latlonpoint (31.288702, 118.360532);//The destination is the latitude and longitude of the Medical College of southern Anhui
Fromandto = new Routesearch.fromandto (start, end);//Instantiate fromandto, literally, where to
Routesearch = new Routesearch (this);//instantiated Routesearch
Routesearch.setroutesearchlistener (this);
}
public void DoClick (view view) {
Switch (View.getid ()) {
Case R.id.btn_bus:
Routesearch.busroutequery busroutequery = new Routesearch.busroutequery (fromandto, Routesearch.busdefault, "Wuhu", 0); /The first parameter represents the start and end point of the path planning, the second parameter represents the bus query mode, the third parameter indicates the bus query city area code, the fourth parameter indicates whether the night train is calculated, and 0 indicates that no calculation
Routesearch.calculatebusrouteasyn (Busroutequery);
Break
Case R.id.btn_car:
Routesearch.driveroutequery driveroutequery = new Routesearch.driveroutequery (Fromandto, RouteSearch.DrivingDefault , NULL, NULL, "");
Routesearch.calculatedriverouteasyn (Driveroutequery);
Break
Case R.id.btn_walk:
Routesearch.walkroutequery walkroutequery = new Routesearch.walkroutequery (fromandto, RouteSearch.WalkDefault);
Routesearch.calculatewalkrouteasyn (Walkroutequery);
Break
}
}
@Override
public void onbusroutesearched (Busrouteresult busrouteresult, int i) {
Buspath Buspath = Busrouteresult.getpaths (). get (0);//Take one of the routes
Amap.clear ();
Busrouteoverlay routeoverlay = new Busrouteoverlay (this, AMap,
Buspath, Busrouteresult.getstartpos (),
Busrouteresult.gettargetpos ());
Routeoverlay.removefrommap ();
Routeoverlay.addtomap ();
Routeoverlay.zoomtospan ();
}
@Override
public void ondriveroutesearched (Driverouteresult driverouteresult, int i) {
Drivepath Drivepath = Driverouteresult.getpaths (). get (0);
Amap.clear ();
Drivingrouteoverlay routeoverlay = new Drivingrouteoverlay (this, AMap,
Drivepath, Driverouteresult.getstartpos (),
Driverouteresult.gettargetpos ());
Routeoverlay.removefrommap ();
Routeoverlay.addtomap ();
Routeoverlay.zoomtospan ();
}
@Override
public void onwalkroutesearched (Walkrouteresult walkrouteresult, int i) {
Walkpath Walkpath = Walkrouteresult.getpaths (). get (0);//Take one of the routes
Amap.clear ()//clear the annotations on the map
Walkrouteoverlay routeoverlay = new Walkrouteoverlay (this, AMap,
Walkpath, Walkrouteresult.getstartpos (),
Walkrouteresult.gettargetpos ());
Routeoverlay.removefrommap ();
Routeoverlay.addtomap ();
Routeoverlay.zoomtospan ();
}
The following are to be carbon
@Override
protected void Onresume () {
Super.onresume ();
Mapview.onresume ();
}
@Override
protected void OnPause () {
Super.onpause ();
Mapview.onpause ();
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
Mapview.ondestroy ();
}
}
Well, over, feel useful to the point of a praise it ~

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.