android定位Demo

本文介绍了一个简单的Android应用,用于获取设备的位置信息。该应用通过设置定位参数并利用LocationManager服务来获取最佳的地理位置提供者,进而获取经纬度坐标,并实时更新显示。
[java]  view plain copy
  1. package com.mapLoacation;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.location.Criteria;  
  6. import android.location.Location;  
  7. import android.location.LocationListener;  
  8. import android.location.LocationManager;  
  9. import android.os.Bundle;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.      Location location;  
  14.      LocationManager locationmanager;  
  15.      String provider;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21.           
  22.         String context=Context.LOCATION_SERVICE;  
  23.         locationmanager = (LocationManager) getSystemService(context);  
  24.         Criteria criteria = new Criteria();  
  25.         criteria.setAccuracy(Criteria.ACCURACY_FINE);  
  26.         criteria.setAltitudeRequired(false);  
  27.         criteria.setBearingRequired(false);  
  28.         criteria.setCostAllowed(true);  
  29.         criteria.setPowerRequirement(Criteria.POWER_LOW);  
  30.         provider = locationmanager.getBestProvider(criteria,true);  
  31.         location = locationmanager.getLastKnownLocation(provider);  
  32.         updateWithNewLocation(location);  
  33.         locationmanager.requestLocationUpdates(provider, 100010, locationlistner);  
  34.     }  
  35.   
  36.     private void updateWithNewLocation(Location location) {  
  37.         String latLongString;  
  38.         TextView myLocationText;  
  39.         myLocationText = (TextView) findViewById(R.id.myLocationText);  
  40.           
  41.         if(location!=null){  
  42.             double lat=location.getLatitude();  
  43.             double lng = location.getLongitude();  
  44.             latLongString="Lat:"+lat+"\nLongitude:"+lng;  
  45.         }else{  
  46.             latLongString="Locaton no found !";  
  47.         }  
  48.         myLocationText.setText(latLongString);  
  49.     }  
  50.       
  51.     LocationListener locationlistner = new LocationListener() {  
  52.           
  53.         @Override  
  54.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  55.             // TODO Auto-generated method stub  
  56.               
  57.         }  
  58.           
  59.         @Override  
  60.         public void onProviderEnabled(String provider) {  
  61.               
  62.         }  
  63.           
  64.         @Override  
  65.         public void onProviderDisabled(String provider) {  
  66.             // TODO Auto-generated method stub  
  67.               
  68.         }  
  69.           
  70.         @Override  
  71.         public void onLocationChanged(Location location) {  
  72.             updateWithNewLocation(location);  
  73.               
  74.         }  
  75.     };  
  76.       
  77. }  

清单文件加入权限:

[html]  view plain copy
  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
  2.       <use-permission android:name="android.permission.INTERNET"/>  

布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/myLocationText"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello"  
  12.     />  
  13.     
  14. </LinearLayout>  


转载于:https://my.oschina.net/limbusnet/blog/74980

使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =(LocationManager) this.getSystemService(Context`.LOCATION_SERVICE); // ``` **2.一般使用LocationManager的getLastKnownLocation(LocationManager.GPS_PROVIDER);方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息 ``` 不过不建议用这种方法,有几点原因: 一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),可以根据位置的距离变化和时间间隔设定,产生位置改变事件的条件,这样可以避免因微小的距离变化而产生大量的位置改变事件 。 二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** LocationManager中设定监听位置变化的代码如下: ``` lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,new MyLocationListener()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值