id="@+id/mapView"
android:
layout_width="fill_parent"android:
layout_height="fill_parent"
android:
clickable="true"
/>
结果见图3-1栅格地图显示效果图:
图3-1栅格地图显示效果图
具体示例请查看参考示例MapView中的GridMapView。
矢量地图显示
将libminimapv320.so复制到工程目录下的libs\armeabi,目录结构见图3-2工程目录结构图:
图3-2工程目录结构图
代码如下:
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.vmapview);
mMapView=(MapView)findViewById(R.id.vmapView);
mMapView.setVectorMap(true);//设置地图为矢量模式
mMapView.setBuiltInZoomControls(true);//设置启用内置的缩放控件
mMapController=mMapView.getController();//得到mMapView的控制权,可以用它控制和驱动平移和缩放
point=newGeoPoint((int)(39.90923*1E6),
(int)(116.397428*1E6));//用给定的经纬度构造一个GeoPoint,单位是微度(度*1E6)
mMapController.setCenter(point);//设置地图中心点
mMapController.setZoom(12);//设置地图zoom级别
}
结果见图3-3
图3-3矢量地图显示效果图
具体示例请查看参考示例MapView中的VectorMapView。
地图覆盖物
概述
所有叠加或覆盖到地图的内容,统称为地图覆盖物。
如标注、矢量图形元素((包括:
折线和多边形和圆))、定位图标等。
覆盖物拥有自己的地理坐标,当您拖动或缩放地图时,它们会相应的移动。
高德地图AndroidAPI提供了如下几种覆盖物:
Overlay:
覆盖物的抽象基类,所有的覆盖物均继承此类的方法,实现用户自定义图层显示。
MyLocationOverlay:
一个负责显示用户当前位置的Overlay。
ItemizedOverlay:
Overlay的一个基类,包含了一个OverlayItem列表,相当于一组分条的Overlay,通过继承此类,将一组兴趣点显示在地图上。
PoiOverlay:
本地搜索图层,提供某一特定地区的位置搜索服务,比如在北京市搜索“天安门”,通过此图层将包含“天安门”关键字的标注显示在地图上。
RouteOverlay:
公交,驾车导航线路图层,将公交、驾车出行方案的路线及关键点显示在地图上。
覆盖物的抽象基类(Overlay)
一般来说,在MapView中添加一个Overlay需要经过以下步骤:
自定义类继承Overlay,并Override其draw()方法可以实现点,线,面等的显示,如果需要点击、按键、触摸等交互操作,还需OverrideonTap()等方法。
添加到MapView的覆盖物中:
publicclassMyOverlayextendsOverlay{
@Override
publicvoiddraw(Canvascanvas,MapViewmapView,booleanshadow){
//TODOAuto-generatedmethodstub
super.draw(canvas,mapView,shadow);
PointscreenPts=newPoint();
mapView.getProjection().toPixels(point,screenPts);
//---addthemarker---
Bitmapbmp=BitmapFactory.decodeResource(
getResources(),R.drawable.da_marker_red);
canvas.drawBitmap(bmp,screenPts.x,screenPts.y-50,null);
PaintpaintText=newPaint();
paintText.setTextSize(18);
paintText.setColor(Color.BLACK);
canvas.drawText("★AMap",screenPts.x,screenPts.y,paintText);//绘制文本
PaintmCirclePaint=newPaint();
mCirclePaint.setAntiAlias(true);
mCirclePaint.setColor(Color.BLUE);
mCirclePaint.setAlpha(50);
mCirclePaint.setStyle(Style.FILL);
canvas.drawCircle(screenPts.x+150,screenPts.y,50,mCirclePaint);
PaintpaintLine=newPaint();
paintLine.setColor(Color.RED);
paintLine.setStrokeWidth(3.0f);
paintLine.setStyle(Paint.Style.STROKE);
canvas.drawLine(screenPts.x-100,screenPts.y,screenPts.x,screenPts.y-200,paintLine);
}
@Override
publicbooleanonTap(GeoPointarg0,MapViewarg1){
//TODOAuto-generatedmethodstub
returnsuper.onTap(arg0,arg1);
}
}
添加到MapView的覆盖物中:
mMapView.getOverlays().add(newMyOverlay());
运行结果见图6-1覆盖物效果图:
当前位置(MyLocationOverlay)
将MyLocationOverlay添加到覆盖物中,能够实现在地图上显示当前位置的图标以及指南针:
MyLocationOverlaymylocTest=newMyLocationOverlay(MylocationDemo.this,map);
mylocTest.enableMyLocation();
mylocTest.enableCompass();//打开指南针
map.getOverlays().add(mylocTest);
运行结果见图6-2当前位置效果图:
图6-2当前位置效果图
分条目覆盖物(ItemizedOverlay)
某个类型的覆盖物,包含多个类型相同、显示方式相同、处理方式相同的项时,使用此类:
自定义类继承ItemizedOverlay,并Override其draw()方法,如果需要点击、按键、触摸等交互操作,还需OverrideonTap()等方法。
classOverItemTextendsItemizedOverlay{
privateListGeoList=newArrayList();
privateDrawablemarker;
privateContextmContext;
privatedoublemLat1=39.9022;//point1纬度
privatedoublemLon1=116.3922;//point1经度
privatedoublemLat2=39.607723;
privatedoublemLon2=116.397741;
privatedoublemLat3=39.917723;
privatedoublemLon3=116.6552;
publicOverItemT(Drawablemarker,Contextcontext){
super(boundCenterBottom(marker));
this.marker=marker;
this.mContext=context;
//用给定的经纬度构造GeoPoint,单位是微度(度*1E6)
GeoPointp1=newGeoPoint((int)(mLat1*1E6),(int)(mLon1*1E6));
GeoPointp2=newGeoPoint((int)(mLat2*1E6),(int)(mLon2*1E6));
GeoPointp3=newGeoPoint((int)(mLat3*1E6),(int)(mLon3*1E6));
//构造OverlayItem的三个参数依次为:
item的位置,标题文本,文字片段
GeoList.add(newOverlayItem(p1,"P1","point1"));
GeoList.add(newOverlayItem(p2,"P2","point2"));
GeoList.add(newOverlayItem(p3,"P3","point3"));
populate();//createItem(int)方法构造item。
一旦有了数据,在调用其它方法前,首先调用这个方法
}
@Override
publicvoiddraw(Canvascanvas,MapViewmapView,booleanshadow){
//Projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标系统之间的变换
Projectionprojection=mapView.getProjection();
for(intindex=size()-1;index>=0;index--){//遍历GeoList
OverlayItemoverLayItem=getItem(index);//得到给定索引的item
Stringtitle=overLayItem.getTitle();
//把经纬度变换到相对于MapView左上角的屏幕像素坐标
Pointpoint=projection.toPixels(overLayItem.getPoint(),null);
//可在此处添加您的绘制代码
PaintpaintText=newPaint();
paintText.setColor(Color.BLACK);
paintText.setTextSize(15);
canvas.drawText(title,point.x-30,point.y-25,paintText);//绘制文本
}
super.draw(canvas,mapView,shadow);
//调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
boundCenterBottom(marker);
}
@Override
protectedOverlayItemcreateItem(inti){
//TODOAuto-generatedmethodstub
returnGeoList.get(i);
}
@Override
publicintsize(){
//TODOAuto-generatedmethodstub
returnGeoList.size();
}
@Override
//处理当点击事件
protectedbooleanonTap(inti){
setFocus(GeoList.get(i));
Toast.makeText(this.mContext,GeoList.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
returntrue;
}
@Override
publicbooleanonTap(GeoPointpoint,MapViewmapView){
//TODOAuto-generatedmethodstub
returnsuper.onTap(point,mapView);
}
}
添加到MapView的覆盖物中:
Drawablemarker=getResources().getDrawable(R.drawable.da_marker_red);//得到需要标在地图上的资源
marker.setBounds(0,0,marker.getIntrinsicWidth(),marker
.getIntrinsicHeight());//为maker定义位置和边界
mMapView.getOverlays().add(newOverItemT(marker,this));//添加ItemizedOverlay实例到mMapView
点击其中一个图标,运行结果见图6-3分条目覆盖效果图:
图6-3分条目覆盖效果图
本地搜索覆盖物(PoiOverlay)
请参见PoiSearch及PoiOverlay的类参考。
驾车、公交路线覆盖物(RouteOverlay)
请参见驾车路线搜索及RouteOverlay和公交路线搜索及RouteOverlay的类参考。
参考资料: