Android LocationManager获取经度与纬度等地理信息

家电修理 2023-07-16 19:17www.caominkang.com电器维修

Android LocationManager获取经度与纬度等地理信息

利用LocationManager实现定位功能  

1 实时更新经度,纬度  

2 根据经度和纬度获取地理信息(比如:国家,街道等)(略过)

MainActivity如下:

package .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.idget.TextVie; 
import android.app.Activity; 
import android.content.Context; 
 
public class MainActivity extends Activity { 
  private Context mContext; 
  private TextVie mTextVie; 
  private LocationManager mLocationManager; 
  private LocationListenerImpl mLocationListenerImpl; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentVie(R.layout.main); 
 init(); 
 initLocationManager(mContext); 
  } 
   
  private void init(){ 
 mContext=this; 
 mTextVie=(TextVie) findVieById(R.id.textVie); 
  } 
 
  private void initLocationManager(Context context){ 
  
 mLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 
  
 //获取可用的位置信息Provider.即passive,ork,gps中的一个或几个 
 List providerList=mLocationManager.getProviders(true); 
 for (Iterator iterator = providerList.iterator(); iterator.hasNext();) { 
   String provider = (String) iterator.next(); 
   System.out.println("provider="+provider); 
 } 
  
  
 //在此采用GPS的方式获取位置信息 
 String GPSProvider=LocationManager.GPS_PROVIDER; 
 Location location=mLocationManager.getLastKnonLocation(GPSProvider); 
 if (location!=null) { 
   double longitude=location.getLongitude(); 
   double altitude=location.getAltitude(); 
   System.out.println("longitude="+longitude+",altitude="+altitude); 
 } else { 
   System.out.println("location==null"); 
 } 
  
 //注册位置监听 
 mLocationListenerImpl=ne LocationListenerImpl(); 
 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, mLocationListenerImpl); 
  } 
   
   
  private class LocationListenerImpl implements LocationListener{ 
 //当设备位置发生变化时调用该方法 
 @Override 
 public void onLocationChanged(Location location) { 
   if (location!=null) { 
 shoLocation(location); 
   } 
 } 
 
 //当provider的状态发生变化时调用该方法.比如GPS从可用变为不可用. 
 @Override 
 public void onStatusChanged(String provider, int status, Bundle extras) { 

 } 
 
 //当provider被打开的瞬间调用该方法.比如用户打开GPS 
 @Override 
 public void onProviderEnabled(String provider) { 

 } 
 
 //当provider被关闭的瞬间调用该方法.比如关闭打开GPS 
 @Override 
 public void onProviderDisabled(String provider) { 

 } 
  
  } 
   
   
  private void shoLocation(Location location) { 
 // 获取经度 
 double longitude = location.getLongitude(); 
 // 获取纬度 
 double altitude = location.getAltitude(); 
 String message="经度为:"+longitude+"n"+"纬度为:"+altitude; 
 mTextVie.setText(message); 
  
  } 
   
  @Override 
  protected void onDestroy() { 
 super.onDestroy(); 
 if (mLocationManager!=null) { 
   mLocationManager.removeUpdates(mLocationListenerImpl); 
 } 
  } 
 
 
   
 
} 

main.xml如下:

 
 
   
 
 

如有疑问请留言或者到本站社区交流讨论,本站关于Android开发的文章还有很多,电脑维修网希望大家多多搜索查阅,感谢阅读,电脑维修网希望能帮助到大家,谢谢大家对本站的支持!

Copyright © 2016-2025 www.caominkang.com 曹敏电脑维修网 版权所有 Power by