androidstudio腾讯地图集成定位绘制行走过的路线

之前项目中集成的是百度地图,然后后面需求改了换成腾讯地图了,个人感觉腾讯地图比百度地图更容易集成,但是腾讯的API文档看着费劲,下面就看一下腾讯地图的流程

一、首先肯定去官方申请key和下载官方demo,这个不多说

然后在配置文件中application下加上

    android:name="TencentMapSDK"
    android:value="申请到的key"/>
二,jar包和so文件

从腾讯地图开放平台下载的demo中获取到jar包和so文件,将jar包复制到libs文件夹下,然后add as Library..,so文件处理是在main文件夹下建一个jniLibs,so文件拷贝到文件夹下


三,添加权限

包括访问jps,网络,网络定位等

android:name="android.permission.ACCESS_NETWORK_STATE"/>
android:name="android.permission.ACCESS_WIFI_STATE"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.READ_PHONE_STATE"/>


android:name="android.permission.ACCESS_FINE_LOCATION" />

android:name="android.permission.ACCESS_COARSE_LOCATION" />

android:name="android.permission.CHANGE_WIFI_STATE" />

android:name="android.permission.CHANGE_NETWORK_STATE" />


四.配置文件

这是xml文件中

< com.tencent.tencentmap.mapsdk.maps.MapView
     android:id = "@+id/map"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent" />

这样的话在自己项目中就能看到地图可以显示了

五.下面看java文件中的代码


mMapView = (MapView) findViewById(R.id.map);
tencentMap = mMapView.getMap();
//设置地图类型为卫星图
tencentMap.setMapType(TencentMap.MAP_TYPE_SATELLITE);

看名字,mapview肯定是view控制器,而Tencentmap类是地图的主类

下面看定位然后把定位到的经纬度存储到集合中,然后将点的集合在地图上添加polyline画出路线


 
  

//定位资源
private DemoLocationSource locationSource;
/**
 *在地图中设置定位资源,保证定位参数传入地图中
 */
//设置显示缩放控件
tencentMap.getUiSettings().setZoomControlsEnabled(false);

locationSource = new DemoLocationSource(this);
//设置我的位置信息来源
tencentMap.setLocationSource(locationSource);
//设置是否显示我的位置
tencentMap.setMyLocationEnabled(true);

/**
 * 腾讯地图定位类,并定位到当前位置,保存时时定位到的点
 */
class DemoLocationSource implements LocationSource, TencentLocationListener {

    private Context mContext;
    private OnLocationChangedListener mChangedListener;
    private TencentLocationManager locationManager;
    private TencentLocationRequest locationRequest;

    public DemoLocationSource(Context context) {
        mContext = context;
        locationManager = TencentLocationManager.getInstance(mContext);
        locationRequest = TencentLocationRequest.create();
        locationRequest.setInterval(4000);        //请求周期
    }

    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
        if (i == TencentLocation.ERROR_OK && mChangedListener != null) {
            Log.e("maplocation", "location: " + tencentLocation.getCity() + " " + tencentLocation.getProvider());

            Log.e("当前的点坐标****", "aaaaaaaaaaaa: " + tencentLocation.getLatitude() + " " + tencentLocation.getLongitude());

            //当前点
            LatLng latLng = new LatLng(tencentLocation.getLatitude(),tencentLocation.getLongitude());


            Location location = new Location(tencentLocation.getProvider());
            location.setLatitude(tencentLocation.getLatitude());
            location.setLongitude(tencentLocation.getLongitude());
            location.setAccuracy(tencentLocation.getAccuracy());



            //定位到当前位置并且设置缩放级别
            tencentMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(tencentLocation.getLatitude(),tencentLocation.getLongitude())));

            if(isStart) {
                if(lalangList.size() == 0) {
                    lalangList.add(latLng);
                }else {
                    if(lalangList.get(lalangList.size()-1).latitude == latLng.latitude && lalangList.get(lalangList.size()-1).longitude == latLng.longitude) {

                    }else {
                        lalangList.add(latLng);
                    }
                }
            }

            mChangedListener.onLocationChanged(location);
        }
    }

    @Override
    public void onStatusUpdate(String arg0, int arg1, String arg2) {

    }

    @Override
    public void activate(OnLocationChangedListener arg0) {
        mChangedListener = arg0;
        int err = locationManager.requestLocationUpdates(locationRequest, this);
        switch (err) {
            case 1:
                setTitle("设备缺少使用腾讯定位服务需要的基本条件");
                break;
            case 2:
                setTitle("manifest 中配置的 key 不正确");
                break;
            case 3:
                setTitle("自动加载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);
    }

}





获取到的定位点集合后就是画了,可以自己设置几秒绘制一次

if(lalangList.size() >= 2) {
    PolylineOptions polyline = new PolylineOptions();
    polyline.width(8).color(0xAAFF0000).setLatLngs(lalangList);
    tencentMap.addPolyline(polyline);
}
 
   


想要其他功能或具体细节请详细参考腾讯地图官方文档和API




你可能感兴趣的:(androidstudio腾讯地图集成定位绘制行走过的路线)