android mapview结合LocationManager应用
来源:互联网 发布:我的世界插件怎么用js 编辑:程序博客网 时间:2024/04/30 12:16
应用需求:
1、提供地图功能
2、提供三种视图:卫星模式、地图模式、我的位置
3、支持多种定位方式:gps、wifi等
4、根据当前的位置,获取服务器上地点信息,并标注在当前屏幕上
解决方案:
1、首先申请google 的 MAP KEY
2、在xml文件中引用mapview,如:<com.google.android.maps.MapView
android:id="@+id/map_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="xxxxxx"//填写申请到key
/>
3、要使用mapview功能,需要在AndroidManifest.xml中申请权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />,同时需要在Activity中引用uses-library
<uses-library android:name="com.google.android.maps" />
4、开始进行构建逻辑。首先Activity需要extends MapActivity。
MapView map = (MapView)findViewById(R.id.mapview);//获得MapView对象
map.getController().setCenter(new GeoPoint(39971036,116314659));//设置地图中心
map.getController().setZoom(10);
以上代码就可以实现显示地图,并以(39971036,116314659)经纬度为中心显示地图。
5、通过对mapview的setSatellite属性进行设置,来达到卫星模式与地图模式的切换。
mapview.setSatellite(true);//卫星视图
mapview.setSatellite(false);//地图视图
6、定位当前位置。定义一个LocationListener,实现其中的onLocationChanged方法,在onLocationChanged中实现定位到当前位置。
public void findWhereYou(Location loc){
GeoPoint gp = getPoint(loc.getLatitude(), loc.getLongitude());
OverlayItem item = new OverlayItem(gp, "您的当前位置:纬度="+gp.getLatitudeE6()+",经度="+gp.getLongitudeE6(), "null");
Drawable marker = getResources().getDrawable(R.drawable.sign);//标记当前位置的图标
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
item.setMarker(marker);
if (mOverlay.size() > 0) {
mOverlay.removeOverlay(0);
}
mOverlay.addOverlay(0, item);//在地图上绘制坐标点时,首先绘制当前坐标点。
mapview.getController().animateTo(gp,new Runnable() {
public void run() {
new HandleTask().execute();
}
});//在地图移到当前位置时,通过AsyncTask来获取服务器端中对应到当前位置附近的坐标点
}
其中mOverla extends 了ItemizedOverlay<OverlayItem>,在android的api这样来解释ItemizedOverlay<OverlayItem>:ItemizedOverlay是Overlay的一个基类,包含了一个OverlayItem列表。 从南到北的处理item,用于绘制、创建平移边界、为每个点绘制标记点,和维护一个焦点选中的item,同时也负责把一个屏幕点击匹配到item上去,分发焦点改变事件给备选的监听器.
上面说到,定义了一个LocationListener,现在需要把这个位置监听器传给LocationManager,才能达到定位的效果。
在定位到我的位置下这样写:
Toast.makeText(this, "正在定位", Toast.LENGTH_LONG).show();
if(!locUtil.requestLocationUpdates(null, 500, 0,locationlistener)) {
Toast.makeText(this, "定位失败", Toast.LENGTH_LONG).show();
}
其中,locUtil是自己针对locationManager封装的一个类。
public class LocationUtil {
private LocationManager locationManager = null;
private List<LocationListener> listenerList = new ArrayList();
public LocationUtil(Context context){
this.locationManager = ((LocationManager)context.getSystemService("location"));
}
public boolean requestLocationUpdates(String provider,long minTime,float minDistance,LocationListener listener){
if(provider == null){
provider = getProvider(null);
}
if(provider != null){
this.locationManager.requestLocationUpdates(provider, minTime, minDistance, listener);
this.listenerList.add(listener);
return true;
}
return false;
}
public String getProvider(Criteria criteria){
if(criteria == null){
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
}
String provider = this.locationManager.getBestProvider(criteria, true);
Log.i("MAPACTIVITY", "provider is :"+provider);
return provider;
}
public Location getLastKnowLocation(){
String provider = getProvider(null);
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public void removeUpdates(LocationListener listener){
this.locationManager.removeUpdates(listener);
}
public void destory(){
for(LocationListener listener :this.listenerList){
this.locationManager.removeUpdates(listener);
}
this.locationManager = null;
}
}
7、以上6步基本上达到需求了。整个应用用到以下几个关键词:mapview、locationManager、GeoPoint、ItemizedOverlay.OnFocusChangeListener()、Locationlistener,通过查找这几个关键词的资料,基本上对于mapview方面的应用没有多大的问题。
8、附带提下:如果要做地图方面的应用,需要考虑一点,有些高端机并不支持google map,这个时候就需要考虑第三方地图提供商了,国内有很多地图提供商,个人认为高德的地图(mapabc)还是不错的,它的api接口与google的兼容,可以说用同一套代码可以加载多个地图。mapabc的优点是:因为是国内的,所以加载速度上要比google map快,api文档比较丰富并且是中文简体的,同时有多个技术讨论群,便于解决一些开发上的问题。还有很重要的一点,google的数据是买它的。 缺点:没有卫星视图,国内的地图一般没有。有些机型,可能会不显示地图,由于4.xxx与5.xxx版本的api名称都有变化,但是官方资料上并没有对两版本进行关联性解释,很容易让人迷惑,有可能是因为5.xxx版本还是bate版的原因吧。
说下百度地图,启用它的原因是,传一个图片过来要上M,并应用程序还大,这个在手机上是不能容忍的。百度地图进军移动终端看来还需要下点功夫了。
- android mapview结合LocationManager应用
- 基于位置的android应用 Location LocationManager
- Android--LocationManager
- android mapview
- android mapview
- Android LocationManager 使用
- Android LocationManager 使用
- Android LocationManager 使用
- Android LocationManager 使用
- Android 位置管理器(LocationManager)
- android LocationManager的使用
- android locationManager定位
- android:LocationManager详解(-)
- 42.Android LocationManager
- Android系统服务--LocationManager
- Android系统服务:LocationManager
- android locationManager定位
- Android LocationManager 定位服务
- Linux下root密码丢失解决办法
- 组框的窗口
- PHP超级配置模块
- 基于asterisk 的计费和直拨实现(For 开源版本ASTCC)
- php.ini 核心配置选项说明
- android mapview结合LocationManager应用
- php配置文件php.ini 中文版
- 认识实习报告
- 诶 为什么一份合适的工作这样难啊
- 新浪微博XSS攻击代码简要解析
- 工具列表及待看资料
- 基于asterisk 的计费和直拨实现(For 开源版本ASTCC)
- 构造Socket
- 妈妈,太空植物园的植物为什么长在地上啊