Detailed explanation of positioning information of sky map

The recent project involves the use of Baidu map. The project team informed to use tianmap instead of Baidu map. One reason is that tianmap is built by the National Bureau of surveying, mapping and geographic information. Enterprises can use its public version for development to provide relevant map information services, which is more authoritative than other maps, Of course, whether the service provided by Tianma map is more authoritative and real-time than other maps is uncertain as an ordinary developer. Another reason is to reduce the application cost. I don't think it's certain whether it can really achieve this goal. After all, the output ability of commercial map is still stronger than that of free version. Let's start the learning path of Tianma Android SDK from the following aspects:

First download the sky map SDK, then add the corresponding jar package and so file to the LIBS folder, and specify the directory of so file as LIBS directory in the build.gradle file of module, as follows:

sourceSets {
    main {
        //指定so文件查找目录是libs目录
        jniLibs.srcDir 'libs'
    }
}

According to the permission configuration specified on the official website, it is found that there are fewer permissions during use. The following is the complete permission list, as follows:

<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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

If the project targetsdkversion is above 23, remember to dynamically apply for relevant danger permissions. At this time, the sky map SDK will be introduced into the project.

First, introduce MapView into the layout. The layout code is as follows:

<com.tianditu.android.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

At this time, the map can be displayed normally without additional configuration. Of course, some map related parameters can be initialized. The common configurations are as follows:

private void initMapView() {
    //启用内置的缩放组件
    mapView.setBuiltInZoomControls(true);
    //得到MapView的控制权,可以用它控制和驱动平移和缩放
    mMapController = mapView.getController();
    //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
    GeoPoint point = new GeoPoint((int) (39.915 * 1E6),(int) (116.404 * 1E6));
    //设置地图中心点
    mMapController.setCenter(point);
    //设置地图缩放级别
    mMapController.setZoom(12);
}

The introduction and map display of Tianma Android SDK are more convenient than Baidu map and Gaode map.

In development, it is often necessary to locate the current location and move the animation from the previous location to the current location. The key class to obtain my location is mylocationoverlay. Through this class, you can easily obtain my location. Because this class is a subclass of overlay, after enabling my location, you need to add an instance of this class to MapView, and my location will be displayed correctly, As for moving to the current location, use the mapcontroller class to get the map controller from MapView and then move to my location. The code reference is as follows:

//创建MyLocationOverlay
myLocationOverlay = new MyLocationOverlay(this,mapView);
//启用指南针位置更新
myLocationOverlay.enableCompass();
//启用我的位置
myLocationOverlay.enableMyLocation();
mapView.addOverlay(myLocationOverlay);
//获得当前位置
mPoint = myLocationOverlay.getMyLocation();
//动画移动到当前位置
mMapController.animateTo(mPoint);

My location is successfully located, so how to modify the default location icon? Here's how to modify the default location icon.

The default positioning icon of the sky map is a small blue dot icon. It is often encountered to modify the positioning icon and the error radius. The latter mainly displays a shaded circle to represent the error range of the current positioning. The class mainly involved in positioning is mylocationoverlay, which is a location overlay class, which is mainly responsible for drawing To get my position and the display of compass, how to modify the default positioning icon is as follows:

This modification method is basically applicable to similar scenarios of modifying the default icon. The key point is to find the relevant class and location, and then do the replacement work. Of course, the corresponding here is to inherit mylocationoverlay, rewrite drawmylocation method, and then replace the corresponding icon. The key code is as follows:

@Override
protected void drawMyLocation(GL10 gl,MapView mapView,Location lastFix,GeoPoint myLocation,long when) {
    //获得屏幕坐标
    Point point = new Point();
    mapView.getProjection().toPixels(myLocation,point);
    //认精度
    float accuracy = getAccuracy();
    //指定精度
    float accuracy = 500;
    //获得实际误差距离
    float distance = mapView.getProjection().metersToEquatorPixels(accuracy);
    AndroidJni.OpenglFillRound(point.x,point.y,(int)distance,0,360,137,170,213,77);
    //创建Drawable
    UtilTextureDrawable drawable = new UtilTextureDrawable(mContext,R.drawable.ic_location,BOUND_TYPE_CENTER);
    drawable.DrawTexture(gl,point,0.0F);
}

At this point, use @ R_ 301_ 2410 @ mllocationoverlay replaces mylocationoverlay to enable my location, and the positioning icon has been changed. Another idea for modifying the default positioning icon here is to use marker to set labels to realize the positioning icon defined by yourself after obtaining the current location. It is found that there is no problem in practice, but the error range can not be easily realized. To sum up, It's better to use the previous idea to solve @ r once and for all_ 301_ 2410 @ icon modification.

To obtain the specific location information, you need to set the listening of the reverse geocoding callback result, obtain the detailed address information, set the coordinate position, start searching the address, and first implement the reverse geocoding result listener, with the code as follows:

/**
 * 逆地理编码回调结果监听
 */
class OnGeoResultListener implements TGeoDecode.OnGeoResultListener {

    @Override
    public void onGeoDecodeResult(TGeoAddress tGeoAddress,int errorCode) {

        if (TErrorCode.OK == errorCode) {
            // 查询点相关信息
            String str = "最近的 poi 名称:" + tGeoAddress.getPoiName() + "\n";
            str += "查询点 Poi 点的方位:" + tGeoAddress.getPoiDirection() + "\n";
            str += "查询点 Poi 点的距离:" + tGeoAddress.getPoiDistance() + "\n";
            str += "查询点行政区名称:" + tGeoAddress.getCity() + "\n";
            str += "查询点地理描述全称:" + tGeoAddress.getFullName() + "\n";
            str += "查询点的地址:" + tGeoAddress.getAddress() + "\n";
            str += "查询点的方位:" + tGeoAddress.getAddrDirection() + "\n";
            str += "查询点的距离:" + tGeoAddress.getAddrDistance() + "\n";
            str += "查询点道路名称:" + tGeoAddress.getRoadName() + "\n";
            str += "查询点与最近道路的距离:" + tGeoAddress.getRoadDistance();
            tvAddress.setText(tGeoAddress.getFullName());
            System.out.println(str);
        } else {
            System.out.println("查询出错:" + errorCode);
        }
    }
}

Then, set the listening for the reverse geocoding result. The code reference is as follows:

//逆地理编码类,根据输入的点坐标,返回相应的地理信息
TGeoDecode tGeoDecode = new TGeoDecode(new OnGeoResultListener());
tGeoDecode.search(mPoint);

If there are no problems above, there must be no problem with the specific location information, but there is no method to obtain the current city name. Getcity() returns the administrative region name rather than the current city name, which must be unfriendly to the need to locate the current city only. This is the end of the first contact with the sky map.

Pay attention to the icons before and after two modifications and the display of error range. The following is the display effect, as follows:

In fact, the use of maps is similar. The basic requirements of the above contents have been completed. Of course, there is another piece of content in map development, which is map annotation. In this step, the contents are pushed out one after another, and my wechat jamanu can be added to communicate and learn from each other.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>