GPS定位

来源:互联网 发布:keep软件的瘦腿好用吗 编辑:程序博客网 时间:2024/04/30 19:02



1.相关类

LocationManager

此类提供了访问系统定位服务,这些服务允许应用程序获得定期更新的设备的地理位置,或当设备进入接近一个给定的地理位置取消一个应用程序指定的Inten。

Context.getSystemService(Context.LOCATION_SERVICE). 可获得定位服务

LocationProvider

一个位置提供者的父类,提供定期报告设备的地理位置功能。每个提供者都有一组可能被使用的Criteria

LocationListener

提供定位信息改变的回调抽象类,用于接收定位相关的改变,回调处理

Location

代表在特定时间的地理位置感知,包含经度和纬度UTC时间戳和可选信息的高度,速度和轴承

Criteria

定位标准类,负责选择一个标准的LocationProvider,并可按需求设置准确性,电量要求,速度,和是否需要支付钱,和提供货报告等。

Geocoder

处理地理编码(将经度和纬度编码为具体的一个街道地址)和逆向地理编码(将街道地址编码为具体经度和纬度)

通过调用List<Address>  getFromLocation/getFromLocationName/返回一个Address对象的List

GeoPoint

一个地理坐标类,有经度和纬度构造

Address

一个代表地址的类,通过一组字符串描述该地址

Locale

一个语言/国家/变体的组合,代表一个场所

2、相关操作

(1)配置提供者的标准Criteria

(2)获取符合所配置Criteria类对象最好的一个LocationProvider的名字:                            String  getBestProviderCriteriaboolean==返回最佳Provider的名字

其实就是“gps==LocationManager.GPS_PROVIDER

(3)获取上一个Location类对象:Location getLastKnownLocationString Provider

(4)获取LocationAddress列表:

GeoCoder类的方法List<Address> getFromLocation[Name]

(5)继承LocationListener,实现相关方法

3.获取经纬度的一般步骤

(1)获取GPS系统服务--getSystemServiceContext.LOCATION_SERVICE

(2)配置Criteria标准--Criteria.set***

(3)获取最佳Provider=gps=LocationManager.GPS_PROVIDER

manager.getBestProvider(Criteria,true)//此处选择最佳Provider可以其他方式,这里用的是GPS,当然亦可以改为其他方式如:LocationManager.NETWORK_PROVIDER

(4)通过Provider获取Location--manager.getLastKnownLocation(Provider);

(5)要求定时更新

---requestLocationUpdates(Provider,minTime,minDistence,LocationListener)

(6)通过Location获取Address列表---Geocoder.getFromLocation(latitude,longi,1)

(7)继承LocationListener监听位置改变

4.代码如下

 

public class MainActivity extends Activity {

private LocationManager locationManager;

private LocationProvider locationProvider;//位置提供者

private Criteria criteria;//一个定位标准

TextView locationText;

private String provider;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

locationText = (TextView)findViewById(R.id.location);

Button GPSButton = (Button)findViewById(R.id.gpsButton);

GPSButton.setOnClickListener(new OnClickListener(){

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//Intent gpsIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS);//打开GPS设置

//startActivity(gpsIntent);

locationInit();

}

});

locationInit();//初始化定位相关

}

 

private void locationInit() {

// TODO Auto-generated method stub

Location location;

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

criteria = new Criteria();//创建一个定位标准类

criteriaInit();//配置标准

provider = locationManager.getBestProvider(criteria, true);//获取最佳Provider的名字

location = locationManager.getLastKnownLocation(provider);//获取Location

newLocation(location);//定位改变

locationManager.requestLocationUpdates(provider, 3000, 0, new LocationListener(){

 

@Override

public void onLocationChanged(Location location) {

// TODO Auto-generated method stub

toast(location);

newLocation(location);//位置改变

}

 

@Override

public void onProviderDisabled(String provider) {

// TODO Auto-generated method stub

//Turn off the GPS

}

 

@Override

public void onProviderEnabled(String provider) {

// TODO Auto-generated method stub

//Turn on the GPS

}

 

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {

// TODO Auto-generated method stub

}

});//3s实时更新

}

 

protected void toast(Location location) {

// TODO Auto-generated method stub

Toast.makeText(this, "位坐标改变,请注意!新的经纬度是:\n经度:"+location.getLongitude()+"\n纬度:"+location.getLatitude()+"\n", Toast.LENGTH_SHORT);

}

 

private void newLocation(Location location)  {

// TODO Auto-generated method stub

 Address address;

 List<Address>addressList = null;

 String latAndLong = null;

 //StringBuffer bs = new StringBuffer();

 String add = null;

 if(location != null){

 //获取经度和纬度

 Double longitude = location.getLongitude();//获取经度

 Double latitude = location.getLatitude();//获取纬度

 latAndLong = "经度:"+longitude+"\n"+" 纬度:"+latitude;

 Log.d("Longitude and Latitude", latAndLong);

 //根据地理环境来编码

 

 try {

 Geocoder geocoder = new Geocoder(this,Locale.getDefault());

 addressList = geocoder.getFromLocation(latitude, longitude, 1);

// addressList = geocoder.getFromLocationName("Beijing", 1);

 StringBuilder bs = new StringBuilder();

 if(addressList.size()>=0){

 address = addressList.get(0);

 for(int i = 0; i<address.getMaxAddressLineIndex();i++){

 bs.append(address.getAddressLine(0)+"\n");

 bs.append(address.getLocality()+"\n");

 bs.append(address.getPostalCode()+"\n");

 bs.append(address.getCountryName());//Country

 add = bs.toString(); 

 }

 }

 

// locationText.setText(add);

} catch (IOException e) {

// TODO Auto-generated catch block

Log.e("获取地址", "获取错误");

}

 

}

 else add = "未找到定位";

locationText.setText("Now your location is\n"+latAndLong+"\n" +add);

}

 

private void criteriaInit() {

// TODO Auto-generated method stub

criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置精确度

criteria.setCostAllowed(false);

criteria.setPowerRequirement(Criteria.POWER_LOW);//设置低电量要求

criteria.setAltitudeRequired(false);//不需求高度信息

criteria.setBearingRequired(false);//不要轴承消息

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

 

}

 

5.打开GPS设置

Intent  myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS ); 

startActivity(myIntent);//打开GPS的设置界面

 

 

0 0
原创粉丝点击