1、添加jar或添加依赖
2、添加清单权限:
<!--地图包、搜索包需要的基础权限-->
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 通过GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
3、依旧清单文件,application里面配置key
<!-- 腾讯地图key配置。如果您key确认无误,却依然授权没有通过,请检查您的key的白名单配置 -->
<meta-data
android:name="TencentMapSDK"
android:value="your key" />
4、layout布局文件中使用地图控件:
<com.tencent.tencentmap.mapsdk.maps.MapView
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
5、按理说,这样子就能看到腾讯地图了,不过还可以给他添加一些配置和定位
private TencentMap mTencentMap;
mTencentMap = mMapView.getMap();
mTencentMap.getUiSettings().setZoomControlsEnabled(false);
mTencentMap.setLocationSource(new DemoLocationSource(this));
mTencentMap.setMyLocationEnabled(true);
6、上面的定位监听和回调DemoLocationSource类:(可以写成内部类)
class DemoLocationSource implements LocationSource, TencentLocationListener {
private Context mContext;
private OnLocationChangedListener mChangedListener;
private TencentLocationManager locationManager;
private TencentLocationRequest locationRequest;
public DemoLocationSource(Context context) {
// TODO Auto-generated constructor stub
mContext = context;
locationManager = TencentLocationManager.getInstance(mContext);
locationRequest = TencentLocationRequest.create();
locationRequest.setInterval(2000);
}
@Override
public void onLocationChanged(TencentLocation arg0, int arg1,
String arg2) {
// TODO Auto-generated method stub
if (arg1 == TencentLocation.ERROR_OK && mChangedListener != null) {
Log.e("maplocation", "location: " + arg0.getCity() + " " + arg0.getProvider());
Location location = new Location(arg0.getProvider());
location.setLatitude(arg0.getLatitude());
location.setLongitude(arg0.getLongitude());
location.setAccuracy(arg0.getAccuracy());
mChangedListener.onLocationChanged(location);
}
}
@Override
public void onStatusUpdate(String arg0, int arg1, String arg2) {
// TODO Auto-generated method stub
}
@Override
public void activate(OnLocationChangedListener arg0) {
// TODO Auto-generated method stub
mChangedListener = arg0;
int err = locationManager.requestLocationUpdates(locationRequest, this);
switch (err) {
case 1:
ActivityTool.showToast(mContext,"设备缺少使用腾讯定位服务需要的基本条件");
break;
case 2:
ActivityTool.showToast(mContext,"manifest 中配置的 key 不正确");
break;
case 3:
ActivityTool.showToast(mContext,"自动加载libtencentloc.so失败");
break;
default:
break;
}
}
@Override
public void deactivate() {
// TODO Auto-generated method stub
locationManager.removeUpdates(this);
mContext = null;
locationManager = null;
locationRequest = null;
mChangedListener = null;
}
public void onPause() {
locationManager.removeUpdates(this);
}
public void onResume() {
locationManager.requestLocationUpdates(locationRequest, this);
}
}
7、销毁
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
mMapView.onDestroy();
mMapView = null;
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
mMapView.onResume();
}
8、ok,本博客结束,具体配置和其他类似的参考腾讯地图SDK官方文档