Complete sample of geographic information such as longitude, latitude, etc. using Locationmanager

Source: Internet
Author: User

Mainactivity as follows:
Package Cc.bb;import Java.util.iterator;import Java.util.list;import android.location.location;import Android.location.locationlistener;import Android.location.locationmanager;import Android.os.Bundle;import Android.widget.textview;import Android.app.activity;import android.content.context;/** * Demo Description: * positioning function with Locationmanager * 1 real-time update of longitude, Latitude * 2 get geographic information based on latitude and longitude (e.g. country, street, etc.) (skip) * * * NOTE: * 0 in the test GPS positioning preferably in a relatively broad space, otherwise affect positioning * 1 use Mlocationmanager.getlastknownlocation (Gpsprovider) gets location is often null.   * Because the device positioning takes a certain amount of time, so put the positioning logic in Locationmanager's Requestlocationupdates () method * * 2 Locationmanager.requestlocationupdates * (String provider, long mintime, float mindistance, Locationlistener listener) * First parameter: location information of the provider, such as GPS * second parameter: Update bit Interval of information, per millisecond * Third parameter: Update location information distance interval, unit meter * Fourth parameter: callback when position information changes * * 3 Locationlistener The most important callback method Onlocationchanged () * When MI  This method is called when both Ntime and mindistance are satisfied. Document Description: * The Mindistance parameter can also be used to control the * frequency of location Updates. If IT is greater than 0 and the * location provider would only send your application a update when * the location has Cha Nged by at least mindistance meters, and * at least mintime milliseconds has passed. * Such as interval time (Mintime) to 3 seconds and moving distance (mindistance) is greater than 5 m * then this method is called. * * 4 Cancel Geo-location updates when activity is OnDestroy (). * * * Permissions configuration: * <uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/> * <uses-permission and Roid:name= "Android.permission.ACCESS_COARSE_LOCATION"/> * <uses-permission android:name= " Android.permission.ACCESS_MOCK_LOCATION "/> * <uses-permission android:name=" Android.permission.INTERNET "/ > */public Class Mainactivity extends Activity {private Context mcontext;private TextView mtextview;private Locationma Nager mlocationmanager;private Locationlistenerimpl mlocationlistenerimpl; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); init (); Initlocationmanager (Mcontext);} private void Init () {mcontext=this;mtextview= (TextView) Findviewbyid (R.id.textview);} private void Initlocationmanager (context context) {mlocationmanager= (Locationmanager) Context.getsystemservice ( Context.location_service);//Gets the available location information provider. That is, one or several list<string> providerlist= in Passive,network,gps Mlocationmanager.getproviders (True); for (iterator<string> Iterator = Providerlist.iterator (); Iterator.hasnext ();) {String Provider = (string) iterator.next (); System.out.println ("provider=" +provider); In this way, the location information is obtained by GPS string Gpsprovider=locationmanager.gps_provider; Location Location=mlocationmanager.getlastknownlocation (gpsprovider); if (location!=null) {double longitude= Location.getlongitude ();d ouble altitude=location.getaltitude (); System.out.println ("longitude=" +longitude+ ", altitude=" +altitude);} else {System.out.println ("Location==null");} Register Location Monitor Mlocationlistenerimpl=new Locationlistenerimpl (); Mlocationmanager.requestlocationupdates ( Locationmanager.gps_provider, 5, mlocationlIstenerimpl);} Private class Locationlistenerimpl implements locationlistener{//call this method when the device location changes @overridepublic void Onlocationchanged (location location) {if (location!=null) {showlocation (location);}} This method is called when the state of the provider changes. For example, GPS from available to unavailable. @Overridepublic void Onstatuschanged (String provider, int status, Bundle Extras) {}//Call this method when provider is opened. For example, the user opens gps@overridepublic void onproviderenabled (String provider) {}// This method is called when the provider is closed. For example, close open gps@overridepublic void onproviderdisabled (String provider) {}}private void showlocation ( Location location) {//Gets the longitude double longitude = location.getlongitude ();//Gets the latitude double altitude = location.getaltitude (); String message= "Longitude:" +longitude+ "\ n" + "Latitude:" +altitude;mtextview.settext (message); @Overrideprotected void OnDestroy () {Super.ondestroy (); if (mlocationmanager!=null) {mlocationmanager.removeupdates ( Mlocationlistenerimpl);}}}

Main.xml as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <textview        android:id=" @+id/textview "        android:layout_width=" Fill_parent "        android:layout_height= "fill_parent"        android:layout_centerinparent= "true"        android:gravity= " Center "/></relativelayout>


Complete sample of geographic information such as longitude, latitude, etc. using Locationmanager

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.