简单使用腾讯地图api,sdk

329 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路​

安卓之显示地图:

首先应该下载所需要的包,详情可以看Android地图SDK | 腾讯位置服务

然后根据页面所说,一步一步来,然后开始在AndroidManifest.xml里面做一下配置

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />\
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />\
  <uses-permission android:name="android.permission.INTERNET" /> <!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 -->\
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 -->\
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->\
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

之后开始创建一个空的activity,先在相应的layout里面弄个地图控件先,下面可以会有点错,不过管他呢

  <com.tenxun.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="match_parent"
        android:layout_height="666dp"
        android:clickable="true">

    </com.tenxun.mapapi.map.MapView>

然后在activity里面写代码

public class txmap extends Activity {
    TencentMap mTencentMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        MapView mapView = new MapView(this);

        mTencentMap = mapView.getMap();
        mTencentMap.getUiSettings().setZoomControlsEnabled(true);
        mTencentMap.setMinZoomLevel(10);
        mTencentMap.setMaxZoomLevel(10);
        mTencentMap.setMyLocationEnabled(true);
        mTencentMap.setLocationSource(new DemoLocationSource(this));


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图

        mTencentMap = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图

    }


    class DemoLocationSource implements LocationSource, TencentLocationListener {
        private Context mContext;
        private OnLocationChangedListener mChangedListener;
        private TencentLocationManager locationManager;
        private TencentLocationRequest locationRequest;

        @SuppressLint("LongLogTag")
        public DemoLocationSource(Context context) {
            // TODO Auto-generated constructor stub
            mContext = context;
            locationManager = TencentLocationManager.getInstance(mContext);

            locationRequest = TencentLocationRequest.create();
            locationRequest.setInterval(2000);

            Log.v("1155555555555555555555555555555555555",locationRequest.toString());
        }

        @Override
        public void onLocationChanged(TencentLocation arg0, int arg1,
                                      String arg2) {
            // TODO Auto-generated method stub
            if (arg1 == TencentLocation.ERROR_OK && mChangedListener != null) {

                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:

                    break;
                case 2:

                    break;
                case 3:

                    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);
        }


    }


}

然后打开模拟器就可以了.


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>创建地图</title>
</head>
<script charset="utf-8" src="https://map.qq.com/api/gljs?v=1.exp&key=**这是你的KEY值,在腾讯地图我的应用哪里找**"></script>
<style type="text/css">
    html,
    body {
        height: 100%;
        margin: 0px;
        padding: 0px;
    }

    #container {
        width: 100%;
        height: 100%;
    }
</style>

<body onload="initMap()">
    <div id="container"></div>
    <script type="text/javascript">
        function initMap() {
            var center = new TMap.LatLng(39.984104, 116.307503);
            //初始化地图
            var map = new TMap.Map("container", {
                rotation: 20,//设置地图旋转角度
                pitch:30, //设置俯仰角度(0~45)
                zoom:12,//设置地图缩放级别
                center: center//设置地图中心点坐标
            });
        }
    </script>
</body>

</html>

​ 而这样就可以了,而你想要根据定位显示地图就通过腾讯的WebService API进行,也在腾讯地图哪里,之后把拿到的值赋给上面的 var center = new TMap.LatLng(39.984104, 116.307503);这里就差不多了,

而如何使用呢,也可以参照官方文档的说明

腾讯地图WebService API 是基于HTTPS/HTTP协议的数据接口,开发者可以使用任何客户端、服务器和开发语言,按照腾讯地图WebService API规范,按需构建HTTPS请求,并获取结果数据(目前支持JSON/JSONP方式返回)

首先也是要获取key,并且在我的应用哪里设置一下,选中webserviceAPI,之后便可以使用。