1、通过googlemap定位
//获取到LocationManager对象
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//如果没有开启位置源,转到‘设置’-‘位置和安全’里勾选使用无线网络,来激活NETWORK_PROVIDER 或 GPS_PROVIDER
if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) &&
!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, "请勾选位置源,无线网络或GPS!", Toast.LENGTH_LONG).show();
MenuActivity.this.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
return;
}
//设置位置查询条件,通过criteria返回符合条件的provider,有可能是wifi provider,也有可能是gps provider
Criteria criteria =new Criteria(); //创建一个Criteria对象
criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置精度,模糊模式,对于DTV地区定位足够了;ACCURACY_FINE,精确模式
criteria.setAltitudeRequired(false); //设置是否需要返回海拔信息,不要求海拔
criteria.setBearingRequired(false); //设置是否需要返回方位信息,不要求方位
criteria.setCostAllowed(true); //设置是否允许付费服
criteria.setPowerRequirement(Criteria.POWER_LOW); //设置电量消耗等级
criteria.setSpeedRequired(false); //设置是否需要返回速度信息
//根据设置的Criteria对象,获取最符合此标准的provider对象
String provider = locationManager.getBestProvider(criteria, true);
Log.d("Location", "provider: "+ provider);
//根据当前provider对象获取最后一次位置信息
Location currentLocation = locationManager.getLastKnownLocation(provider);
//如果位置信息为null,则请求更新位置信息
if(currentLocation ==null){
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
}
//直到获得最后一次位置信息为止,如果未获得最后一次位置信息,则显示默认经纬度
//每隔10秒获取一次位置信息
while(true){
currentLocation = locationManager.getLastKnownLocation(provider);
if(currentLocation !=null){
Log.d("Location", "Latitude: "+ currentLocation.getLatitude());
Log.d("Location", "location: "+ currentLocation.getLongitude());
//长时间的监听位置更新可能导致耗电量急剧上升,一旦获取到位置后,就停止监听
locationManager.removeUpdates(locationListener);
break;
}else{
Log.d("Location", "Latitude: "+0);
Log.d("Location", "location: "+0);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e("Location", e.getMessage());
}
}
//解析地址并显示
Geocoder geoCoder =new Geocoder(this);
try {
double latitude = currentLocation.getLatitude();
double longitude = currentLocation.getLongitude();
List list = geoCoder.getFromLocation(latitude, longitude, 2);
if(list!=null && !list.isEmpty()){
//取第一个地址就可以
Address address = list.get(0);
//getCountryName 国家
//getAdminArea 省份
//getLocality 城市
//getSubLocality 区
//getFeatureName 街道
Toast.makeText(MenuActivity.this, address.getCountryName() + address.getAdminArea() + address.getLocality() + address.getSubLocality() + address.getFeatureName(), Toast.LENGTH_LONG).show();
System.out.println(address.getAddressLine(0)+" "+address.getAddressLine(1)+" "+address.getAddressLine(2)+" "+address.getFeatureName());
}
}
catch (IOException e) {
Toast.makeText(MenuActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
provider也可以不使用Criteria获取,而是通过静态方式获得:
provider = LocationManager.NETWORK_PROVIDER; //WIFI 定位
provider = LocationManager.NETWORK_GPS; //GPS 定位
/*
* 创建位置监听器
*/
private LocationListener locationListener =new LocationListener(){
//位置发生改变时调用
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged");
Log.d("Location", "onLocationChanged Latitude"+ location.getLatitude());
Log.d("Location", "onLocationChanged location"+ location.getLongitude());
}
//provider失效时调用
@Override
public void onProviderDisabled(String provider) {
Log.d("Location", "onProviderDisabled");
}
//provider启用时调用
@Override
public void onProviderEnabled(String provider) {
Log.d("Location", "onProviderEnabled");
}
//状态改变时调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location", "onStatusChanged");
}
};
在AndroidManifest.xml中还要加上权限:
如果同时使用NETWORK_PROVIDER和GPS_PROVIDER,那么只需要获取ACCESS_FINE_LOCATION 的权限,因为ACCESS_FINE_LOCATION 包含了两种提供者所需要的权限。(ACCESS_COARSE_LOCATION的权限仅仅包括NETWORK_PROVIDER所需的权限。)
ACCESS_NETWORK_STATE 允许应用访问网络上的信息
精确定位,包括最新位置比较,请参考 http://913.iteye.com/blog/1489637
最后,介绍一下根据地址解析经纬度,经手机上测试,效果非常好,经纬度非常精确。
/**
* 将地址解析成经纬度
*/
public void addrParse(String addr){
addr = "china haikou";
Geocoder geoCoder =new Geocoder(this);
try{
List addressList = geoCoder.getFromLocationName(addr, 5);
if(addressList != null && addressList.size() > 0){
double latitude = addressList.get(0).getLatitude();
double longitude = addressList.get(0).getLongitude();
System.out.println("latitude: " + latitude + " longitude: " + longitude);
}
else{
System.out.println("addrParse: get locatoin error");
}
}
catch (IOException e) {
Toast.makeText(MenuActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
2、百度地图定位
使用Android自带的LocationManager和Location获取位置的时候,经常会有获取的location为null的情况,并且操作起来也不是很方便,在这个Demo里我使用了百度地图API中的定位SDK,可以一次性获取当前位置经纬度以及详细地址信息,还可以获取周边POI信息,同时可以设定位置通知点,当到达某一位置时,发出通知信息等方式来告知用户。jar包下载以及官方文档请参照:百度定位SDK,前提是需要注册百度开发者账号。
下面来看看定位的基本原理,目前,定位SDK可以通过GPS、基站、Wifi信号进行定位。基本定位流程如下图所示,当应用程序向定位SDK发起定位请求时,定位SDK会根据当前的GPS、基站、Wifi信息生成相对应的定位依据。然后定位SDK会根据定位依据来进行定位。如果需要,定位SDK会向定位服务器发送网络请求。定位服务器会根据请求的定位依据推算出对应的坐标位置,然后根据用户的定制信息,生成定位结果返回给定位SDK。
到官方下载jar文件后添加到工程,工程目录截图如下:
注意要把locSDK_2.4.jar添加到当天工程,右键jar文件-Build path-Add to。。。
上代码
布局文件:
01 |
xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:layout_width = "fill_parent" |
04 |
android:layout_height = "fill_parent" |
05 |
android:orientation = "vertical" > |
08 |
android:id = "@+id/btn_start" |
09 |
android:layout_width = "fill_parent" |
10 |
android:layout_height = "wrap_content" |
11 |
android:layout_marginTop = "20dp" |
12 |
android:text = "Start" /> |
15 |
android:id = "@+id/tv_loc_info" |
16 |
android:layout_width = "fill_parent" |
17 |
android:layout_height = "wrap_content" |
18 |
android:textSize = "18sp" /> |
配置文件:
01 |
xml version = "1.0" encoding = "utf-8" ?> |
02 |
< manifest xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
package = "com.ericssonlabs" |
04 |
android:versionCode = "1" |
05 |
android:versionName = "1.0" > |
07 |
< uses-sdk android:minSdkVersion = "8" /> |
09 |
< permission android:name = "android.permission.BAIDU_LOCATION_SERVICE" > |
12 |
< uses-permission android:name = "android.permission.BAIDU_LOCATION_SERVICE" > |
14 |
< uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" > |
16 |
< uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" > |
18 |
< uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" > |
20 |
< uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" > |
22 |
< uses-permission android:name = "android.permission.CHANGE_WIFI_STATE" > |
24 |
< uses-permission android:name = "android.permission.READ_PHONE_STATE" > |
26 |
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" > |
28 |
< uses-permission android:name = "android.permission.INTERNET" /> |
29 |
< uses-permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > |
31 |
< uses-permission android:name = "android.permission.READ_LOGS" > |
35 |
android:icon = "@drawable/ic_launcher" |
36 |
android:label = "@string/app_name" > |
38 |
android:name = ".LocationDemoActivity" |
39 |
android:label = "@string/app_name" > |
41 |
< action android:name = "android.intent.action.MAIN" /> |
43 |
< category android:name = "android.intent.category.LAUNCHER" /> |
48 |
android:name = "com.baidu.location.f" |
49 |
android:enabled = "true" |
50 |
android:permission = "android.permission.BAIDU_LOCATION_SERVICE" |
51 |
android:process = ":remote" > |
53 |
< action android:name = "com.baidu.location.service_v2.4" /> |
实现代码:
001 |
public class LocationDemoActivity extends Activity { |
002 |
private TextView locationInfoTextView = null ; |
003 |
private Button startButton = null ; |
004 |
private LocationClient locationClient = null ; |
005 |
private static final int UPDATE_TIME = 5000 ; |
006 |
private static int LOCATION_COUTNS = 0 ; |
009 |
public void onCreate(Bundle savedInstanceState) { |
010 |
super .onCreate(savedInstanceState); |
011 |
setContentView(R.layout.main); |
013 |
locationInfoTextView = (TextView) this .findViewById(R.id.tv_loc_info); |
014 |
startButton = (Button) this .findViewById(R.id.btn_start); |
017 |
locationClient = new LocationClient( this ); |
019 |
LocationClientOption option = new LocationClientOption(); |
020 |
option.setOpenGps( true ); |
021 |
option.setCoorType( "bd09ll" ); |
022 |
option.setPriority(LocationClientOption.NetWorkFirst); |
023 |
option.setProdName( "LocationDemo" ); |
024 |
option.setScanSpan(UPDATE_TIME); |
025 |
locationClient.setLocOption(option); |
028 |
locationClient.registerLocationListener( new BDLocationListener() { |
031 |
public void onReceiveLocation(BDLocation location) { |
033 |
if (location == null ) { |
036 |
StringBuffer sb = new StringBuffer( 256 ); |
037 |
sb.append( "Time : " ); |
038 |
sb.append(location.getTime()); |
039 |
sb.append( "\nError code : " ); |
040 |
sb.append(location.getLocType()); |
041 |
sb.append( "\nLatitude : " ); |
042 |
sb.append(location.getLatitude()); |
043 |
sb.append( "\nLontitude : " ); |
044 |
sb.append(location.getLongitude()); |
045 |
sb.append( "\nRadius : " ); |
046 |
sb.append(location.getRadius()); |
047 |
if (location.getLocType() == BDLocation.TypeGpsLocation){ |
048 |
sb.append( "\nSpeed : " ); |
049 |
sb.append(location.getSpeed()); |
050 |
sb.append( "\nSatellite : " ); |
051 |
sb.append(location.getSatelliteNumber()); |
052 |
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ |
053 |
sb.append( "\nAddress : " ); |
054 |
sb.append(location.getAddrStr()); |
057 |
sb.append( "\n检查位置更新次数:" ); |
058 |
sb.append(String.valueOf(LOCATION_COUTNS)); |
059 |
locationInfoTextView.setText(sb.toString()); |
063 |
public void onReceivePoi(BDLocation location) { |
068 |
startButton.setOnClickListener( new OnClickListener() { |
071 |
public void onClick(View v) { |
072 |
if (locationClient == null ) { |
075 |
if (locationClient.isStarted()) { |
076 |
startButton.setText( "Start" ); |
077 |
locationClient.stop(); |
079 |
startButton.setText( "Stop" ); |
080 |
locationClient.start(); |
088 |
locationClient.requestLocation(); |