Android 高德地图

简单版的高德地图
Android 高德地图_第1张图片
注册登录
Android 高德地图_第2张图片
注册成功后进入个人中心
Android 高德地图_第3张图片
点击我的应用
Android 高德地图_第4张图片
点击创建新应用
Android 高德地图_第5张图片
可以填入你想取的名称 选择类型
Android 高德地图_第6张图片
Android 高德地图_第7张图片
Android 高德地图_第8张图片

获取SHA1码
Android 高德地图_第9张图片
Android 高德地图_第10张图片

在此附上GitHub地址

https://github.com/zzzzzjson/GaodeDemo
需要导的jar在GitHub中

需要加的权限

 
    
    
    
    
    
    
    

    
    

    
    

    
    
    
    
    
    
    
    
    
    
    

    
    

    
    

    
    
    
    

清单文件中

  
  
 

项目的根路径
Android 高德地图_第11张图片
写入一个工具类

public class LocationUtil implements AMapLocationListener {
    private AMapLocationClient aMapLocationClient;
    private AMapLocationClientOption clientOption;
    private ILocationCallBack callBack;

    public void startLocate(Context context){
        aMapLocationClient = new AMapLocationClient(context);

        //设置监听回调
        aMapLocationClient.setLocationListener(this);

        //初始化定位参数
        clientOption = new AMapLocationClientOption();
        clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        clientOption.setNeedAddress(true);
        clientOption.setOnceLocation(false);
        //设置是否强制刷新WIFI,默认为强制刷新
        clientOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        clientOption.setMockEnable(false);
        //设置定位间隔
        clientOption.setInterval(2000);
        aMapLocationClient.setLocationOption(clientOption);

        aMapLocationClient.startLocation();
    }

    //完成定位回调
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if(aMapLocation != null){
            if(aMapLocation.getErrorCode() == 0){
                //定位成功完成回调
                String country = aMapLocation.getCountry();
                String province = aMapLocation.getProvince();
                String city = aMapLocation.getCity();
                String district = aMapLocation.getDistrict();
                String street = aMapLocation.getStreet();
                double lat = aMapLocation.getLatitude();
                double lgt = aMapLocation.getLongitude();

                callBack.callBack(country + province + city + district + street,lat,lgt,aMapLocation);
            }else{
                //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }

    /**
     * 自定义图标
     * @return
     */
    public MarkerOptions getMarkerOption(String str, double lat, double lgt){
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));
        markerOptions.position(new LatLng(lat,lgt));
        markerOptions.title(str);
        markerOptions.snippet("纬度:" + lat + "   经度:" + lgt);
        markerOptions.period(100);

        return markerOptions;
    }

    public interface ILocationCallBack{
        void callBack(String str, double lat, double lgt, AMapLocation aMapLocation);
    }

    public void setLocationCallBack(ILocationCallBack callBack){
        this.callBack = callBack;
    }

}

高德的代码

public class MainActivity extends AppCompatActivity implements LocationSource {
    private MapView mapView;
    private AMap aMap;
    private OnLocationChangedListener mListener = null;//定位监听器
    private LocationUtil locationUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.MapView);
        mapView.onCreate(savedInstanceState);

        init();
    }

    private void init() {
        if(aMap == null){
            aMap = mapView.getMap();
        }

        setLocationCallBack();

        //设置定位监听
        aMap.setLocationSource(this);
        //设置缩放级别
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
        //显示定位层并可触发,默认false
        aMap.setMyLocationEnabled(true);
    }

    private void setLocationCallBack(){
        locationUtil = new LocationUtil();
        locationUtil.setLocationCallBack(new LocationUtil.ILocationCallBack() {
            @Override
            public void callBack(String str,double lat,double lgt,AMapLocation aMapLocation) {

                //根据获取的经纬度,将地图移动到定位位置
                aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat,lgt)));
                mListener.onLocationChanged(aMapLocation);
                //添加定位图标
                aMap.addMarker(locationUtil.getMarkerOption(str,lat,lgt));
            }
        });
    }

    //定位激活回调
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;

        locationUtil.startLocate(getApplicationContext());
    }

    @Override
    public void deactivate() {
        mListener = null;
    }

    @Override
    protected void onPause() {
        super.onPause();
        //暂停地图的绘制
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁地图
        mapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //重新绘制加载地图
        mapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}

还有一个反编译地理位置的代码,效果图就不上了

public class AddrChangeActivity extends AppCompatActivity implements View.OnClickListener, GeocodeSearch.OnGeocodeSearchListener {

    private EditText Addr_Edit;
    private Button Zheng_Btn;
    private EditText JinDU_Edit;
    private EditText WeiDu_Edit;
    private Button Fan_Btn;
    private TextView Get_Text;
    private GeocodeSearch geocodeSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addr_change);
        initView();
        //构造 GeocodeSearch 对象,并设置监听。
        geocodeSearch = new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(this);
//通过GeocodeQuery设置查询参数,调用getFromLocationNameAsyn(GeocodeQuery geocodeQuery) 方法发起请求。
//address表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode都ok
//        GeocodeQuery query = new GeocodeQuery(address, "010");
//        geocoderSearch.getFromLocationNameAsyn(query);

    }

    private void initView() {
        Addr_Edit = (EditText) findViewById(R.id.Addr_Edit);
        Zheng_Btn = (Button) findViewById(R.id.Zheng_Btn);
        JinDU_Edit = (EditText) findViewById(R.id.JinDU_Edit);
        WeiDu_Edit = (EditText) findViewById(R.id.WeiDu_Edit);
        Fan_Btn = (Button) findViewById(R.id.Fan_Btn);
        Get_Text = (TextView) findViewById(R.id.Get_Text);

        Zheng_Btn.setOnClickListener(this);
        Fan_Btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Zheng_Btn:
                String addr = Addr_Edit.getText().toString().trim();
                if (addr.isEmpty()) {
                    Toast.makeText(this, "请输入地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                //参数1:addr 地址值 参数2:规定一个区域
                GeocodeQuery query = new GeocodeQuery(addr, null);
                geocodeSearch.getFromLocationNameAsyn(query);
                break;
            case R.id.Fan_Btn:
                String jingdu = JinDU_Edit.getText().toString().trim();
                String weidu = WeiDu_Edit.getText().toString().trim();
                if (jingdu.isEmpty() || weidu.isEmpty()) {
                    Toast.makeText(this, "请输入经纬度", Toast.LENGTH_SHORT).show();
                    return;
                }
                //这个是经纬度查询的类
                LatLonPoint point = new LatLonPoint(Double.parseDouble(jingdu), Double.parseDouble(weidu));
                RegeocodeQuery regeocodeQuery = new RegeocodeQuery(point, 2000000000, GeocodeSearch.AMAP);
                GeocodeSearch reSe = new GeocodeSearch(this);
                reSe.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
                    @Override
                    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
                        Log.e("onRegeocodeSearched", "onRegeocodeSearched");
                        RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
                        Get_Text.setText(address.getFormatAddress()+"地址");
                    }

                    @Override
                    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

                    }
                });
                geocodeSearch.getFromLocationAsyn(regeocodeQuery);
                break;
        }
    }

    //把经纬度转换成地址
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        Log.e("onRegeocodeSearched", "onRegeocodeSearched");
        RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
        Get_Text.setText(address.getFormatAddress());
    }

    //是吧地址转换成经度纬度
    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        Log.e("onGeocodeSearched", "onGeocodeSearched");
        //从查出来的结果集 得到地址对象
        GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0);
        //从地址对象里面得到 经纬度的类
        LatLonPoint latLonPoint = address.getLatLonPoint();
        //从这个point取经纬度即可
        Get_Text.setText("经度是:" + latLonPoint.getLongitude() + ",纬度是:" + latLonPoint.getLatitude());
    }
}

反编译的xml




    

        

        

你可能感兴趣的:(Android 高德地图)