一、概述
这是利用android的API来定位的,如果想利用百度地图API定位,请查看我的另一篇博客 http://blog.youkuaiyun.com/sq_bang/article/details/51946370。
二、Geocoder
1、作用:Geocoder可以将一个地址转变为经纬度,或者将经纬度转变为地址
2、地址转换为经纬度示例:
Geocoder geocoder = new Geocoder(this);
//参数1:搜索的地址,参数2:地址返回最大数
//搜索过程中会返回许多相同名称的位置,比如搜“沃尔玛”,则返回各地的沃尔玛位置,所以同时限定最大数来获取这个地址的位置。
List<Address> list = geocoder.getFromLocationName(“沃尔玛超市”, 5);
if(list != null && list.size() > 0){
double lat = list.get(0).getLatitude();
double lng = list.get(0).getLongitude();
}
三、百度静态地图
1、百度静态地图定义:http://lbsyun.baidu.com/index.php?title=static/static-1
2、百度静态地图使用接口:http://api.map.baidu.com/staticimage?center=……
例如:http://api.map.baidu.com/staticimage?center=113.32208016605,23.137769529767
&width=1000&height=600&zoom=14
然后返回一张地图图片
3、获取静态地图示例:
WebView browser = (WebView) findViewById(R.id.web);
String url = “http://api.map.baidu.com/staticimage?center=113.32208016605,
23.137769529767&width=1000&height=600&zoom=14”;
browser.loadUrl(url);
四、定位
1、添加如下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
2、在Activity的生命周期方法里面设置注册监听和注销监听及时释放资源。
3、示例
package com.sqb.androidlocation;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
/**
* 添加如下权限:
* <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
* <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
*
* @author sqb
*/
public class MainActivity extends Activity {
LocationManager locationManager;
List<String> providerlist;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
// 获取LocationManager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000, 0, mLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3000, 0, mLocationListener);
//locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,3000, 0, mLocationListener);
}
@Override
protected void onPause() {
super.onPause();
// 注销监听,释放资源
locationManager.removeUpdates(mLocationListener);
}
private void showLocation(Location location) {
String latLongString;
double lat = 0;
double lng = 0;
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
} else {
latLongString = "无法获取地理信息,请稍后...";
}
Toast.makeText(getApplicationContext(), latLongString,
Toast.LENGTH_SHORT).show();
tv.setText(tv.getText() + "\n" + "--自动更新--");
tv.setText(tv.getText() + "\n" + latLongString);
}
public final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
showLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
showLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
参考资料
1、location为null:http://blog.sina.com.cn/s/blog_5d5996d00100w7ed.html