Android Baidu Map development of automatic positioning location and fixed location for driving, walking, bus route search

Source: Internet
Author: User

Recently followed the Baidu Map API map development, first learned the path search, for the known coordinates of two points for driving, bus, walk three kinds of path search (bus path run no effect, to learn), and later learned the positioning function, to obtain the latitude and longitude of their location, But when the two together to automatically obtain their own location of latitude and longitude and the fixed location of the path search, but can not get out, because the beginning of the two in the two classes will always take the latitude and longitude values, and then write the two to the same class, and finally take the latitude and longitude value, also run out. The latitude and longitude values need to be obtained in the Bdlocationlistener onreceivelocation, because the longitude and latitude values can be taken directly, so you can search for them in the path. Lori said some estimates only I can read ....

Summarize your own implementation process (only the main one):

1. Initializing variables

2. Initialize the Bmapmanager in front of the Setcontentview.

3. Get Mapcontroller, identify a point where the center of the Mapview is located, (this step is not very small), if the lack of mapview will show to the default address (which seems to be the land of Beijing) to affect the path of the subsequent starting position of the search.

4. Generate loctionclient and use Locationclientoption to make simple settings. Call Locationclient.start ();
Locationclient.requestlocation (); To locate the position, use the callback to register the listener with Locatiopnclient, and obtain latitude and longitude in the callback method onreceivelocation Using Mkplannode to set the starting position point (the latitude and longitude that has been acquired) and the end point, you can initialize the Mksearch, and replicate the callback method in its parameter listener Ongetwalkingrouteresult, Ongettransitrouteresult , Ongetdrivingrouteresult, last Mksearch.walkingsearch (null, SATRT, NULL, end);
Mksearch.drivingsearch (NULL, SATRT, NULL, end);
Mksearch.transitsearch ("Shanghai", SATRT, end); (not run out, to learn)
You'll be able to search the route.

The code is as follows

Package Com.example.lockposi;

Import com.baidu.location.BDLocation;
Import Com.baidu.location.BDLocationListener;
Import com.baidu.location.LocationClient;
Import com.baidu.location.LocationClientOption;
Import Com.baidu.mapapi.BMapManager;
Import Com.baidu.mapapi.MKGeneralListener;
Import com.baidu.mapapi.map.MKEvent;
Import Com.baidu.mapapi.map.MapController;
Import Com.baidu.mapapi.map.MapView;
Import Com.baidu.mapapi.map.RouteOverlay;
Import Com.baidu.mapapi.search.MKAddrInfo;
Import Com.baidu.mapapi.search.MKBusLineResult;
Import Com.baidu.mapapi.search.MKDrivingRouteResult;
Import Com.baidu.mapapi.search.MKPlanNode;
Import Com.baidu.mapapi.search.MKPoiInfo;
Import Com.baidu.mapapi.search.MKPoiResult;
Import Com.baidu.mapapi.search.MKSearch;
Import Com.baidu.mapapi.search.MKSearchListener;
Import Com.baidu.mapapi.search.MKSuggestionResult;
Import Com.baidu.mapapi.search.MKTransitRouteResult;
Import Com.baidu.mapapi.search.MKWalkingRouteResult;
Import Com.baidu.platform.comapi.basestruct.GeoPoint;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast;

public class Mainactivity extends Activity {

TextView TextView;
Locationclient locationclient;
Double latitude,longitude;
Private Mapview Mapview;
Bmapmanager Bmapmanager;
Static final String api_key= "07418aec69baab7104c6230a6120c580dffaeebb";

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Bmapmanager=new Bmapmanager (Getapplicationcontext ());
Bmapmanager.init (api_key,new Mkgenerallistener () {

@Override
public void ongetpermissionstate (int arg0) {
TODO auto-generated Method Stub
if (arg0==mkevent.error_permission_denied) {
Toast.maketext (Getapplicationcontext (), "Please enter the correct key", "()." Show ();

}
}

@Override
public void ongetnetworkstate (int arg0) {
TODO auto-generated Method Stub
if (arg0==mkevent.error_network_connect) {
Toast.maketext (Getapplicationcontext (), "Your network has gone wrong", "()." Show ();
}
}
});
Setcontentview (R.layout.activity_main);
Mapview = (Mapview) This.findviewbyid (R.id.bmapview);
Mapview.setbuiltinzoomcontrols (TRUE);

Mapcontroller Mmapcontroller = Mapview.getcontroller ();

         GeoPoint GeoPoint = new GeoPoint ((int) (31.243319 * 1E6), 
                 (int) ( 121.509075 * 1E6));  
         mmapcontroller.setcenter (geoPoint);  
         mmapcontroller.setzoom ( );

textview= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Locationclient=new locationclient (Getapplicationcontext ());
Locationclientoption locationclientoption=new locationclientoption ();
Locationclientoption.setopengps (TRUE);
Locationclientoption.setcoortype ("Bd09ll");
Locationclientoption.setpriority (Locationclientoption.networkfirst);
Locationclientoption.setscanspan (5000);
Locationclient.setlocoption (locationclientoption);
Locationclient.start ();
Locationclient.requestlocation ();
Toast.maketext (Getapplicationcontext (), string.valueof (latitude), (). Show ();

Locationclient.registerlocationlistener (New Bdlocationlistener () {

@Override
public void Onreceivepoi (Bdlocation arg0) {
TODO auto-generated Method Stub
if (arg0==null) {
Return
}
Latitude=arg0.getlatitude ();
Longitude=arg0.getlongitude ();
StringBuilder sbuilder=new StringBuilder ();
Sbuilder.append ("Latitude:" +string.valueof (Arg0.getlatitude ())). Append ("Longitude:" +string.valueof ( Arg0.getlongitude ()));
Textview.settext (Sbuilder.tostring ());
}

@Override
public void Onreceivelocation (Bdlocation arg0) {
TODO auto-generated Method Stub
if (arg0==null) {
Return
}
Latitude=arg0.getlatitude ();
Longitude=arg0.getlongitude ();
StringBuilder sbuilder=new StringBuilder ();
Sbuilder.append ("Latitude:" +arg0.getlatitude ()). Append ("Longitude:" +arg0.getlongitude ());
Textview.settext (Sbuilder.tostring ());
Mkplannode satrt=new Mkplannode ();
Satrt.pt=new GeoPoint ((int) (31.205889 * 1E6),
(int) (121.637942 * 1E6));
Mkplannode end=new Mkplannode ();
End.pt=new GeoPoint ((int) (31.243319 * 1E6), (int) (121.509075 * 1E6));
Final Mksearch mksearch=new Mksearch ();
Mksearch.init (Bmapmanager, New Mksearchlistener () {

private int intidx;

     @Override
     public void Ongetwalkingrouteresult ( Mkwalkingrouteresult arg0, int arg1) {
     //TODO auto-generated method stub
 & nbsp;    if (arg0==null) {
       return;
     }
      routeoverlay routeoverlay=new routeoverlay ( Mainactivity.this, Mapview);
      routeoverlay.setdata (Arg0.getplan (0). Getroute (0));
      mapview.getoverlays (). Add (Routeoverlay);
      Mapview.refresh ();
    }

@Override
public void Ongettransitrouteresult (Mktransitrouteresult arg0, int arg1) {
TODO auto-generated Method Stub
if (arg0==null) {
Return
}
Routeoverlay routeoverlay=new Routeoverlay (Mainactivity.this, Mapview);
Routeoverlay.setdata (Arg0.getplan (0). Getroute (0));
Mapview.getoverlays (). Add (Routeoverlay);
Mapview.refresh ();

}

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

}

@Override
public void Ongetpoiresult (Mkpoiresult arg0, int arg1, int arg2) {
TODO auto-generated Method Stub
if (arg2! = 0 | | arg0 = = NULL) {
Toast.maketext (Mainactivity.this, "Sorry, no results found", Toast.length_long). Show ();
Return
}
Find bus route POI node
Mkpoiinfo curpoi = null;
int totalpoinum = Arg0.getnumpois ();
for (intidx = 0; intidx<totalpoinum; intidx++) {
Curpoi = Arg0.getpoi (INTIDX);
if (2 = = Curpoi.epoitype) {
Break
}
}
Mksearch.buslinesearch ("Shanghai", Curpoi.uid);
}

@Override
public void Ongetpoidetailsearchresult (int arg0, int arg1) {
TODO auto-generated Method Stub

}

@Override
public void Ongetdrivingrouteresult (Mkdrivingrouteresult arg0, int arg1) {
TODO auto-generated Method Stub
if (arg0==null) {
Return
}
Routeoverlay routeoverlay=new Routeoverlay (Mainactivity.this, Mapview);
Routeoverlay.setdata (Arg0.getplan (0). Getroute (0));
Mapview.getoverlays (). Add (Routeoverlay);
Mapview.refresh ();
}

@Override
public void Ongetbusdetailresult (Mkbuslineresult arg0, int arg1) {
TODO auto-generated Method Stub
if (arg1! = 0 | | arg0 = = NULL) {
Toast.maketext (Mainactivity.this, "Sorry, no results found", Toast.length_long). Show ();
Return
}
Routeoverlay routeoverlay = new Routeoverlay (mainactivity.this, Mapview); Only one scenario is shown here as an example
Routeoverlay.setdata (Arg0.getbusroute ());
Mapview.getoverlays (). Clear ();
Mapview.getoverlays (). Add (Routeoverlay);
Mapview.refresh ();
Mapview.getcontroller (). Animateto (Arg0.getbusroute (). Getstart ());
}

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

}
});

Mksearch.walkingsearch (NULL, SATRT, NULL, end);
Mksearch.drivingsearch (NULL, SATRT, NULL, end);
Mksearch.transitsearch ("Shanghai", SATRT, end);

}

});

}

}

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.