Android 天地图定位

定位工具类
注意请先依赖对应天地图的jar文件之后再使用此类。天地图资源包下载
如果链接无法正常跳转下载,直接把地址复制到浏览器下载:
http://download.tianditu.com/download/mobile/Tiandituapi3.0.2%2820190103-02%29.zip

import android.content.Context;
import android.location.Location;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MyLocationOverlay;
import javax.microedition.khronos.opengles.GL10;

/**
 * 天地图定位
 */
public class SkyLandMapLocation {

    /**
     * 上下文对象
     */
    private Context context;

    /**
     * 地图对象
     */
    private MapView mapView;

    /**
     * 定位辅助对象
     */
    private LocationOverlay overlay;

    /**
     * 是否开启一次性定位
     */
    private boolean onceLocation = true;


    /**
     * 构造函数
     * @param context
     * @param mapView
     */
    public SkyLandMapLocation(Context context, MapView mapView) {
        this.context = context;
        this.mapView = mapView;
    }


    /**
     * 获取定位覆盖物
     * @return
     */
    public LocationOverlay getOverlay() {
        return overlay;
    }

    /**
     * 设置定位覆盖物
     * @param overlay
     */
    public void setOverlay(LocationOverlay overlay) {
        this.overlay = overlay;
    }

    /**
     * 是否一次性定位
     * @return
     */
    public boolean isOnceLocation() {
        return onceLocation;
    }

    /**
     * 是否一次性定位
     * @param onceLocation
     */
    public void setOnceLocation(boolean onceLocation) {
        this.onceLocation = onceLocation;
    }

    /**
     * 获取定位监听
     * @return
     */
    public OnSkyLangLocationChangedListener getListener() {
        return listener;
    }

    /**
     * 设置定位监听
     * @param listener
     */
    public void setListener(OnSkyLangLocationChangedListener listener) {
        this.listener = listener;
    }

    /**
     * 开始定位
     */
    public void start() {
        overlay = new LocationOverlay(context, mapView);
        mapView.addOverlay(overlay);
        overlay.enableCompass();
        overlay.enableMyLocation();
        overlay.setGpsFollow(true);
        overlay.setVisible(true);
    }

    /**
     * 停止定位
     */
    public void stop() {
        if (overlay != null) {
            if (overlay.isCompassEnabled()) {
                overlay.disableCompass();
            }
            if (overlay.isMyLocationEnabled()) {
                overlay.disableMyLocation();
            }
        }
    }

    private class LocationOverlay extends MyLocationOverlay {

        public LocationOverlay(Context context, MapView mapView) {
            super(context, mapView);
        }

        @Override
        public void onLocationChanged(Location location) {
            super.onLocationChanged(location);
            if (listener != null) {
                listener.onSkyLangLocationChanged(location);
            }
            if (onceLocation) {
                stop();
            }
        }
    }

    /**
     * 定位监听接口
     */
    public OnSkyLangLocationChangedListener listener;

    public interface OnSkyLangLocationChangedListener {
        void onSkyLangLocationChanged(Location location);
    }

}

使用
注意6.0以上的权限问题
Manifest.permission.WRITE_EXTERNAL_STORAGE
Manifest.permission.READ_EXTERNAL_STORAGE
Manifest.permission.ACCESS_FINE_LOCATION
Manifest.permission.ACCESS_COARSE_LOCATION

   SkyLandMapLocation location = new SkyLandMapLocation(this, mapView);
   location.setListener(this);
   location.setOnceLocation(true);
   location.start();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        if (location != null) {
            location.stop();
        }
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值