天地图是由国家地理信息测绘局出的,以jar包的形式提供各种地图服务和数据,如地图展示、标注、定位等等。下面介绍几种简单的用法。
一、集成步奏
1、导入jar包
将API文件tiandituapi.jar拷贝到工程根目录下,并在工程属性->Java Build Path->Libraries中选择”Add External JARs”, tiandituapi.jar,确定后返回,这样您就可以在您的程序中使用API了。
2、添加权限
在AndroidManiFest文件中添加以下权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3、在XML文件中添加地图显示组件。
<com.tianditu.android.maps.MapView
android:id="@+id/map_tian_location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
4、在Activity的onCreate方法中初始化地图。
MapView mMapView = (MapView) findViewById(map_tian_location_map);
//设置启用内置的缩放控件
mMapView.setBuiltInZoomControls(true);
//得到mMapView的控制权,可以用它控制和驱动平移和缩放
MapController mMapController = mMapView.getController();
//用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
//设置地图中心点
mMapController.setCenter(point);
//设置地图zoom级别
mMapController.setZoom(12);
这样就可以显示地图了,其他详细的功能实现参考官方API:http://lbs.tianditu.com/api-new/mobile/guide.html#map3
二、其他功能实现。
1、在地图上添加覆盖物点
package com.yongnuo.mccms.utils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.ItemizedOverlay;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.OverlayItem;
import com.yongnuo.mccms.entity.Parts;
import com.yongnuo.mccms.ui.activity.PartsInfoActivity;
import java.util.ArrayList;
import java.util.List;
public class DrawPointUtils extends ItemizedOverlay {
private Context mContext;
private MapView mMapView;
private ArrayList mPointList;
private ArrayList gpList = new ArrayList();
private Drawable mDrawable;
private List mCovers;
public DrawPointUtils(Drawable drawable, Context context, MapView mapview,
ArrayList list, List covers) {
super(drawable);
mContext = context;
mMapView = mapview;
mPointList = list;
mDrawable = drawable;
mCovers = covers;
for (int i = 0; i < list.size(); i++) {
Parts p = list.get(i);
double lon = Double.valueOf(p.getLongitude());
double lat = Double.valueOf(p.getLatitude());
GeoPoint gp = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
gpList.add(new OverlayItem(gp, "T" + i, "point" + i));
}
populate();
if (gpList != null && gpList.size() > 0) {
mMapView.getController().setCenter(gpList.get(0).getPoint());
}
}
@Override
protected boolean onTap(int arg0) {
super.onTap(arg0);
IntentUtils.startActivity(mContext, PartsInfoActivity.ACTION_FLAG_PATRS,mCovers.get(arg0), PartsInfoActivity.class);
return true;
}
@Override
protected OverlayItem createItem(int arg0) {
if (gpList != null) {
return gpList.get(arg0);
}
return null;
}
@Override
public int size() {
if (gpList != null) {
return gpList.size();
}
return 0;
}
public void addItem(OverlayItem overlayItem) {
overlayItem.setMarker(mDrawable);
gpList.add(overlayItem);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent arg1, MapView arg2) {
if (keyCode == KeyEvent.KEYCODE_BACK)
System.exit(0);
return super.onKeyUp(keyCode, arg1, arg2);
}
}
2、在地图上根据点画出一条线
package com.yongnuo.mcms.utils;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.Overlay;
import com.tianditu.android.maps.Projection;
import java.util.ArrayList;
public class DrawLineUtil {
private ArrayList mGeoPointList;
private MapView mMapView;
private Context mContext;
public DrawLineUtil(Context context, ArrayList list,
MapView mapView) {
mContext = context;
mGeoPointList = list;
mMapView = mapView;
if(list != null && list.size() > 0){
DrawOverlay mDrivingOverlay = new DrawOverlay(context);
mMapView.getOverlays().add(mDrivingOverlay);
mMapView.getController().animateTo(list.get(list.size() - 1));
mMapView.postInvalidate();
}
}
class DrawOverlay extends Overlay {
public DrawOverlay(Context context) {
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
ArrayList PL = new ArrayList();
Point[] pointScr = convertPoints(mapView.getProjection(),mGeoPointList);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
Path path = new Path();
path.moveTo(pointScr[0].x, pointScr[0].y);
Point point = null;
Projection projection = mapView.getProjection();
for (int i = 1; i < pointScr.length; i++) {
path.lineTo(pointScr[i].x, pointScr[i].y);
point = new Point();
projection.toPixels(mGeoPointList.get(i), point);
PL.add(point);
}
canvas.drawPath(path, paint);
path.reset();
// Drawable mDrawableStart = (Drawable) mContext.getResources().getDrawable(R.mipmap.icon_route_start);
// Drawable mDrawableEnd = (Drawable) mContext.getResources().getDrawable(R.mipmap.icon_route_end);
// //起点
// int wid = mDrawableStart.getIntrinsicWidth();
// int height = mDrawableStart.getIntrinsicHeight();
// if(PL != null && PL.size() > 0){
// mDrawableStart.setBounds(PL.get(0).x - wid / 2, PL.get(0).y - height, PL.get(0).x + wid / 2,PL.get(0).y);
// mDrawableStart.draw(canvas);
// }
//
// //终点
// wid = mDrawableEnd.getIntrinsicWidth();
// height = mDrawableEnd.getIntrinsicHeight();
// if(PL != null && PL.size() > 0){
// Point endPoint = PL.get(PL.size() - 1);
// mDrawableEnd.setBounds(endPoint.x - wid / 2, endPoint.y - height, endPoint.x + wid / 2, endPoint.y);
// mDrawableEnd.draw(canvas);
// }
}
private Point[] convertPoints(Projection prj, ArrayList points) {
Point[] ptsRet = new Point[points.size()];
for (int i = 0; i < ptsRet.length; i++) {
ptsRet[i] = prj.toPixels(points.get(i), null);
}
return ptsRet;
}
}
}
3、使用方法
画点使用方法
Marker marker = getResources().getDrawable(R.mipmap.ic_pointblack);
List mPointList = new ArrayList();
mMapView.getOverlays().add(new DrawPointUtils(marker, this, mMapView, mPointList, covers));
画线使用方法
ArrayList list = new ArrayList();
new DrawLineUtil(this,list,mMapView);