Android Combat-location-based services (get latitude and longitude of your location + Baidu map)

Source: Internet
Author: User

Need to use the Locationmanager class implementation, the following directly look at the code:

<?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:o rientation= "vertical" >    <textview        android:id= "@+id/edit"        android:layout_width= "Fill_parent"        android:layout_height= "Wrap_content"/></linearlayout>


. Java code is as follows:

Package Org.lxh.demo;import Java.util.list;import Android.app.activity;import android.content.context;import Android.location.location;import Android.location.locationlistener;import Android.location.LocationManager; Import Android.os.bundle;import android.util.log;import Android.widget.textview;import android.widget.Toast;public Class Hello extends Activity {private TextView textview;private Locationmanager locationmanager;private String provider; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview ( R.layout.main); Set the layout manager log.d ("111111", "4444444") to be used, TextView = (TextView) Findviewbyid (r.id.edit); Locationmanager = ( Locationmanager) Getsystemservice (Context.location_service); list<string> providerlist = Locationmanager.getproviders (TRUE);//gets all available location providers if (Providerlist.contains ( Locationmanager.gps_provider)) {PROVIDER = Locationmanager.gps_provider;} else if (Providerlist.contains ( Locationmanager.network_provider)) {pRovider = Locationmanager.network_provider; LOG.D ("111111", "3333333");} else {Toast.maketext (this, "No location provider to use!", Toast.length_short). Show (); return;} Location location = locationmanager.getlastknownlocation (provider), if (location! = null) {showlocation (location);} Locationmanager.requestlocationupdates (provider, 1,locationlistener);}  private void Showlocation {String crrrentpositionstring = "Latitude is" + location.getlatitude () + "\ n" + "Longitude is" + location.getlongitude (); Textview.settext (crrrentpositionstring);} Locationlistener Locationlistener = new Locationlistener () {public void onstatuschanged (String provider, int status, Bund Le extras) {//Todo auto-generated method stub}public void onproviderenabled (String provider) {//Todo auto-generated meth od stub}public void onproviderdisabled (String provider) {//TODO auto-generated method stub}public void onlocationchanged (Location location) {showlocation (location);//update current device information LOG.D ("111111", "222222 ");}; protected void OnDestroy () {super. OnDestroy (); if (Locationmanager! = null) {locationmanager.removeupdates ( Locationlistener);}};}


The Androidmanifest.xml code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Org.lxh.demo "android:versioncode=" 1 "android:versionname=" 1.0 "><USES-SDK android: Minsdkversion= "Ten"/><uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/><!-- Claim permissions--><application android:icon= "@drawable/icon" android:label= "@string/app_name" ><activity android: Name= ". Hello "android:label=" @string/app_name "><intent-filter><action android:name=" Android.intent.action.MAIN "/><category android:name=" Android.intent.category.LAUNCHER "/></ Intent-filter></activity></application></manifest>


Run the instance can display your current latitude and longitude, below we mainly introduce how to use Baidu map:

First you have to apply for an API Key, the specific method can be Baidu, all finished after the jar package download down:

To import a development package:

Then set the layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent ">    <com.baidu.mapapi.map.mapview        android:id=" @+id /map_view "        android:layout_width=" match_parent "        android:layout_height=" Match_parent "        android: Clickable= "true"/></linearlayout>


Then modify the Mainactivity.java file:

Package Com.example.baidumaptest;import Java.util.list;import Android.app.activity;import android.content.Context; Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.location.location;import Android.location.locationmanager;import Android.os.bundle;import Android.widget.toast;import Com.baidu.mapapi.bmapmanager;import Com.baidu.mapapi.map.locationdata;import Com.baidu.mapapi.map.MapController; Import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.mylocationoverlay;import Com.baidu.mapapi.map.popupclicklistener;import Com.baidu.mapapi.map.popupoverlay;import Com.baidu.platform.comapi.basestruct.geopoint;public class Mainactivity extends Activity {private Bmapmanager Manager ;p rivate mapview mapview;private locationmanager locationmanager;private String provider; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); manager = new Bmapmanager (this);//API Key needs to be replaced by your own manager.init ("Shvpottipzfonpd3hckc5sit", null); Setcontentview (r.layout.activity_main); Mapview = (Mapview) Findviewbyid (R.id.map_view); Mapview.setbuiltinzoomcontrols (true); Locationmanager = (Locationmanager) getsystemservice (Context.LOCATION_ SERVICE),//Get all available location providers list<string> Providerlist = Locationmanager.getproviders (true); Providerlist.contains (Locationmanager.gps_provider)) {PROVIDER = Locationmanager.gps_provider;} else if ( Providerlist.contains (Locationmanager.network_provider)) {PROVIDER = Locationmanager.network_provider;} else {// When there is no available location provider, pop-up Toast prompts the user toast.maketext (this, "No locations provider to use", Toast.length_short). Show (); return;} Location location = locationmanager.getlastknownlocation (provider), if (location! = null) {NavigateTo (location);}} private void NavigateTo (location location) {Mapcontroller controller = Mapview.getcontroller ();// Set zoom level Controller.setzoom (16); GeoPoint point = new GeoPoint ((int) (Location.getlatitude () * 1E6), (int) (Location.getlongitude () * 1E6));//Set Map Center points CONTR Oller.setcenter(point); Mylocationoverlay mylocationoverlay = new Mylocationoverlay (Mapview); Locationdata locationdata = new Locationdata ();//Specify my location Locationdata.latitude = Location.getlatitude (); Locationdata.longitude = Location.getlongitude (); Mylocationoverlay.setdata (Locationdata); MapView.getOverlays (). Add (mylocationoverlay);//Refresh to make the new cover effective Mapview.refresh ();  Popupoverlay pop = new Popupoverlay (Mapview, New Popupclicklistener () {@Overridepublic void onclickedpopup (int index) {// The corresponding picture of the Click event Toast.maketext (Mainactivity.this, "you clicked Button" + Index, Toast.length_short). Show ();}); /create a Bitmap array of length 3 bitmap[] bitmaps = new Bitmap[3];try {//Read three pictures into memory bitmaps[0] = Bitmapfactory.decoderesource ( Getresources (), r.drawable.left); bitmaps[1] = Bitmapfactory.decoderesource (Getresources (), R.drawable.middle); BITMAPS[2] = Bitmapfactory.decoderesource (Getresources (), r.drawable.right);} catch (Exception e) {e.printstacktrace ();} Pop.showpopup (bitmaps, point, 18);} @Overrideprotected void OnDestroy () {Mapview.destroy (); if (manager = null) {Manager.destroy (); manager = null;} Super.ondestroy ();} @Overrideprotected void OnPause () {mapview.onpause (); if (manager! = null) {manager.stop ();} Super.onpause ();} @Overrideprotected void Onresume () {mapview.onresume (); if (manager! = null) {Manager.start ();} Super.onresume ();}}


Configure the Androidmanifest.xml file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.baidumaptest "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SD K android:minsdkversion= "android:targetsdkversion=" "/> <uses-permission android:name=" Androi D.permission.access_network_state "/> <uses-permission android:name=" Android.permission.INTERNET "/> <u Ses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= " Android.permission.CHANGE_WIFI_STATE "/> <uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/> <uses-permission android:name=" Android.permission.WRITE_SETTINGS "/> <uses-permission androi D:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= "android.permission.CALL_ PHONE "/> <uses-permission android:name=" android.permission.Access_fine_location "/> <uses-permission android:name=" Android.permission.ACCESS_COARSE_LOCATION "/> < Uses-permission android:name= "Android.permission.ACCESS_MOCK_LOCATION"/> <uses-permission android:name= " Android.permission.ACCESS_GPS "/> <application android:allowbackup=" true "android:icon=" @drawable/I            C_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android:name= "com.example.baidumaptest.MainActivity" android:label= "@string/app_name" > < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category an Droid:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </app Lication></manifest>

Running an instance:


Android Combat-location-based services (get latitude and longitude of your location + Baidu map)

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.