android location 位置定位

实现功能:定位当前位置并显示详细信息,扩展了googleMap地图


package com.ld.gps;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;

public class AppLocationGpsActivity extends Activity {

private static final int TWO_MINUTES = 1000 * 60 * 2;
private LocationManager locationManager;

private String providerName;

private Location currentBestLocation;

private Location location;

private Address address;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取 LocationManager 服务

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

// 获取 Location Provider

providerName=getProviderName();

// 如果未设置位置源,打开 GPS 设置界面

openGPS();

// 获取位置

currentBestLocation= locationManager.getLastKnownLocation(providerName);

location=new Location(LocationManager.GPS_PROVIDER);

if(isBetterLocation(location,currentBestLocation)){
// 显示位置信息到文字标签

updateWithNewLocation(location);
providerName=location.getProvider();

}else{
// 显示位置信息到文字标签

updateWithNewLocation(currentBestLocation);

}


// 注册监听器 locationListener ,第 2 、 3 个参数可以控制接收 gps 消息的频度以节省电力。第 2 个参数为毫秒,

// 表示调用 listener 的周期,第 3 个参数为米 , 表示位置移动指定距离后就调用 listener

locationManager.requestLocationUpdates(providerName, 2000, 10,locationListener);
}

// 判断是否开启 GPS ,若未开启,打开 GPS 设置界面

private void openGPS() {

if (locationManager
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)

|| locationManager
.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)

) {

Toast.makeText(this, " 位置源已设置! ", Toast.LENGTH_SHORT).show();

return;

}

Toast.makeText(this, " 位置源未设置! ", Toast.LENGTH_SHORT).show();

// 转至 GPS 设置界面

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

startActivityForResult(intent, 0);

}

// 获取 Location Provider

private String getProviderName() {

// 构建位置查询条件

Criteria criteria = new Criteria();

// 查询精度:高

criteria.setAccuracy(Criteria.ACCURACY_FINE);

// 是否查询海拨:否

criteria.setAltitudeRequired(false);

// 是否查询方位角 : 否

criteria.setBearingRequired(false);

// 是否允许付费:是

criteria.setCostAllowed(true);

// 电量要求:低

criteria.setPowerRequirement(Criteria.POWER_LOW);

// 返回最合适的符合条件的 provider ,第 2 个参数为 true 说明 , 如果只有一个 provider 是有效的 , 则返回当前
// provider

return locationManager.getBestProvider(criteria, true);

}

// Gps 消息监听器

private final LocationListener locationListener = new LocationListener() {

// 位置发生改变后调用

public void onLocationChanged(Location location) {

updateWithNewLocation(location);

}

// provider 被用户关闭后调用

public void onProviderDisabled(String provider) {

updateWithNewLocation(null);

}

// provider 被用户开启后调用

public void onProviderEnabled(String provider) {
}

// provider 状态变化时调用

public void onStatusChanged(String provider, int status,

Bundle extras) {
}

};

// Gps 监听器调用,处理位置信息

private void updateWithNewLocation(Location location) {

String latLongString;

TextView myLocationText = (TextView) findViewById(R.id.text);

if (location != null) {

double lat = location.getLatitude();

double lng = location.getLongitude();

latLongString = " 纬度 :" + lat + "\n 经度 :" + lng;

} else {

latLongString = " 无法获取地理信息 ";

}
List<Address> addrList=getAddressbyGeoPoint(location);
String addr="当前详细地址:";
if(addrList!=null && !addrList.isEmpty())
addr+=parseAddr(addrList.get(0));
myLocationText.setText(" 您当前的位置是 :\n" +

latLongString + "\n" + addr);

}

private String parseAddr(Address address){

return address.getAddressLine(0)+address.getAddressLine(1)+address.getAddressLine(2)+address.getFeatureName();
}
// 获取地址信息

private List<Address> getAddressbyGeoPoint(Location location) {

List<Address> result = null;

// 先将 Location 转换为 GeoPoint

// GeoPoint gp =getGeoByLocation(location);

try {

if (location != null) {

// 获取 Geocoder ,通过 Geocoder 就可以拿到地址信息

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

result = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**
* Determines whether one Location reading is better than the current
* Location fix
*
* @param location
* The new Location that you want to evaluate
* @param currentBestLocation
* The current Location fix, to which you want to compare the new
* one
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}

// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;

// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be
// worse
} else if (isSignificantlyOlder) {
return false;
}

// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;

// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());

// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值