Android Baidu map positioning function

Source: Internet
Author: User

Don't say a word. Create a new Android project directly: location, then take a minute to apply for a key, and then just copy the Baidu positioning demo.

1: First add permissions in Androidmanifest.xml

<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name= "Android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name= "Android.permission.READ_LOGS" >
</uses-permission>
<uses-permission android:name= "Android.permission.VIBRATE"/>
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>

2: Add a line of code to the application node using the application you wrote in the Androidmanifest.xml file

Android:name= "Com.baidu.baidulocationdemo.LocationApplication"

3: Copy the following on the application node

<service
            Android:name= "COM.BAIDU.LOCATION.F"
            android:enabled= "true"
            android:process= ": Remote",
            < Intent-filter>
                <action android:name= " com.baidu.location.service_v2.2,
                </ACTION>
& nbsp           </INTENT-FILTER>,
        </SERVICE>
  & nbsp     <!--Meta-data need to be written in application-->
        <meta-data
    & nbsp       android:name= "Com.baidu.lbsapi.API_KEY"
            Android:value = "xaxmr5wgrssc9atv8gzovq96"/>

Note that you need to change the key for your application.

4: Configuration Even if OK, then write code, who call us the yard? First look at your own definition of class Myapplication.java

Package Com.example.location;import Com.baidu.location.bdlocation;import Com.baidu.location.BDLocationListener; Import Com.baidu.location.locationclient;import Com.baidu.location.poi;import Android.app.application;import Android.app.service;import Android.os.vibrator;import Android.util.log;import Android.widget.TextView;import    Java.util.list;public class MyApplication extends application {public locationclient mlocationclient;    Public Mylocationlistener Mmylocationlistener;    Public TextView mlocationresult,logmsg;    Public TextView Trigger,exit; Public vibrator mvibrator; @Overridepublic void OnCreate () {super.oncreate (); mlocationclient = new Locationclient (        This.getapplicationcontext ());        Mmylocationlistener = new Mylocationlistener ();        Mlocationclient.registerlocationlistener (Mmylocationlistener); Mvibrator = (vibrator) getapplicationcontext (). Getsystemservice (Service.vibrator_service);} /** * Implement real-time position callback monitoring */public class Mylocationlistener ImplemEnts Bdlocationlistener {@Override public void onreceivelocation (bdlocation location) {Stringbu            Ffer sb = new StringBuffer (256);            Sb.append ("Time:");            Sb.append (Location.gettime ());            Sb.append ("\nerror Code:");            Sb.append (Location.getloctype ());            Sb.append ("\nlatitude:");            Sb.append (Location.getlatitude ());            Sb.append ("\nlontitude:");            Sb.append (Location.getlongitude ());            Sb.append ("\nradius:");            Sb.append (Location.getradius ());                if (location.getloctype () = = Bdlocation.typegpslocation) {//GPS location result Sb.append ("\nspeed:");                Sb.append (Location.getspeed ());//unit: km/h sb.append ("\nsatellite:");                Sb.append (Location.getsatellitenumber ());                Sb.append ("\nheight:");            Sb.append (Location.getaltitude ());//unit: M Sb.append ("\ndirection:");    Sb.append (Location.getdirection ());                Sb.append ("\naddr:");                Sb.append (Location.getaddrstr ());                Sb.append ("\ndescribe:");            Sb.append ("GPS positioning success");                } else if (location.getloctype () = = Bdlocation.typenetworklocation) {//Network location result Sb.append ("\naddr:");                Sb.append (Location.getaddrstr ());                Carrier information Sb.append ("\noperationers:");                Sb.append (Location.getoperators ());                Sb.append ("\ndescribe:");            Sb.append ("Network positioning success");                } else if (location.getloctype () = = Bdlocation.typeofflinelocation) {//Offline location result Sb.append ("\ndescribe:");            Sb.append ("Offline positioning is successful, offline positioning results are also valid");                } else if (location.getloctype () = = Bdlocation.typeservererror) {sb.append ("\ndescribe:");            Sb.append ("Server-side network location failed, can be feedback IMEI number and the approximate location of the time to [email protected], will be traced to the cause"); } else if (locatIon.getloctype () = = bdlocation.typenetworkexception) {sb.append ("\ndescribe:");            Sb.append ("Network different causes location failure, please check the network is unobstructed");                } else if (location.getloctype () = = bdlocation.typecriteriaexception) {sb.append ("\ndescribe:");            Sb.append ("Unable to obtain an effective location based on the location failure, is generally due to the cause of the phone, in flight mode is generally caused by this result, you can try to focus on the mobile phone");            } sb.append ("\nlocationdescribe:");//Position Semantics information sb.append (Location.getlocationdescribe ()); list<poi> list = Location.getpoilist ();//Poi Information if (list! = null) {sb.append ("\npoilis                T size =: ");                Sb.append (List.size ());                    for (Poi p:list) {sb.append ("\npoi=:");                Sb.append (P.getid () + "" + p.getname () + "" + P.getrank ());            }} logmsg (Sb.tostring ());        LOG.I ("Baidulocationapidem", sb.tostring ());    }}/** * Display request String * @param str */public void logmsg (String str) {try {if (Mlocationresult! = null) Mlocationresul        T.settext (str);        } catch (Exception e) {e.printstacktrace (); }    }}

Here is directly from the Baidu positioning SDK demo copied over, this does not need to knock, because it has no technical and logic to say.

5:mainactivity.java

Package Com.example.location;import Com.baidu.location.locationclient;import Com.baidu.location.locationclientoption;import Com.baidu.location.locationclientoption.locationmode;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class MainActivity extends Activity {private TextView tv_locaiton_result;private locationclient mlocationclient;private locationmode tempMode = Locationmode.hight_accuracy;private String tempcoor= "gcj02"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mLocationClient = ((MyApplication) getapplication ()). Mlocationclient;tv_locaiton_result = (TextView) Findviewbyid (r.id.tv_locaiton_ Result);((MyApplication) getapplication ()). Mlocationresult = Tv_locaiton_result;initlocation ();}        private void Initlocation () {locationclientoption option = new Locationclientoption ();    Option.setlocationmode (Tempmode);//selectable, default high precision, set positioning mode, high accuracy, low power, device only    Option.setcoortype (Tempcoor);//optional, default GCJ02, set the returned positioning result coordinate system, int span=5000; Option.setscanspan (span);//optional, default 0, that is, to locate only once, set the interval to initiate a location request is greater than or equal to 1000ms is valid Option.setopengps (TRUE);//optional, default false, Set whether to use GPS option.setlocationnotify (TRUE);//optional, default false to set whether GPS results are output according to 1S1 frequency when GPS is active option.setignorekillprocess (true);//optional, by default true, the location SDK inside is a service, and put in a separate process, set whether to kill the process at stop, the default does not kill Option.setenablesimulategps (false);//optional, Default false to set whether the GPS simulation results need to be filtered by default, Option.setisneedlocationdescribe (TRUE),//optional, default false, set whether the location semantics result is required, Can be obtained in the bdlocation.getlocationdescribe, the result is similar to "near the Beijing Tian ' an gate" option.setisneedlocationpoilist (TRUE);//optional, default false,        Set whether you need POI results, you can get mlocationclient.setlocoption (option) in Bdlocation.getpoilist;    Mlocationclient.start (); }}

OK, the location is good, but this is the location to get to the address, the map shows the next time you get



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Baidu map positioning function

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.