Search in sources :

Example 1 with AMapLocationListener

use of com.amap.api.location.AMapLocationListener in project Collar by CodeZsx.

the class PostActivity method startLocation.

private void startLocation() {
    requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE);
    mBinding.tvAddress.setText("定位中...");
    AMapLocationListener mLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    Message message = new Message();
                    message.what = MESSAGE_LOCATION_RESULT;
                    message.obj = aMapLocation.getAddress();
                    mHandler.sendMessage(message);
                } else {
                    // 定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
                }
            }
        }
    };
    mLocationClient = new AMapLocationClient(getApplicationContext());
    mLocationClient.setLocationListener(mLocationListener);
    // 初始化AMapLocationClientOption对象
    AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    // 设置高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    // //设置低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(WIFI和基站定位)
    // mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
    // //设置仅用设备定位模式:不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位,自v2.9.0版本支持返回地址描述信息
    // mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors);
    // 设置获取3s内精度最高的一次定位结果
    mLocationOption.setOnceLocationLatest(true);
    // 设置是否返回地址信息,默认返回地址信息
    mLocationOption.setNeedAddress(true);
    // 给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    // 启动定位
    mLocationClient.startLocation();
}
Also used : AMapLocation(com.amap.api.location.AMapLocation) AMapLocationClient(com.amap.api.location.AMapLocationClient) Message(android.os.Message) AMapLocationClientOption(com.amap.api.location.AMapLocationClientOption) AMapLocationListener(com.amap.api.location.AMapLocationListener)

Example 2 with AMapLocationListener

use of com.amap.api.location.AMapLocationListener in project ttdj by soonphe.

the class HomeFragment method initLocation.

private void initLocation() {
    // 声明AMapLocationClient类对象
    mLocationClient = new AMapLocationClient(this.getContext());
    // 配置定位参数
    AMapLocationClientOption option = new AMapLocationClientOption();
    // 高精度定位模式:会同时使用网络定位和GPS定位
    option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    option.setOnceLocation(true);
    mLocationClient.setLocationOption(option);
    // 设置 定位回调监听器
    mLocationClient.setLocationListener(new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    /*
                        //基础定位信息
                         amapLocation.getCountry();//国家信息
                         amapLocation.getProvince();//省信息
                         amapLocation.getCity();//城市信息
                         amapLocation.getDistrict();//城区信息
                         amapLocation.getStreet();//街道信息
                         amapLocation.getStreetNum();//街道门牌号信息
                         //获取定位时间
                         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                         Date date = new Date(amapLocation.getTime());
                         */
                    String city = aMapLocation.getCity();
                    String cou = aMapLocation.getDistrict();
                    Log.e("onLocationChanged", "city: " + city);
                    Log.e("onLocationChanged", "district: " + cou);
                    String location = StringUtils.extractLocation(city, cou);
                    tvCity.setText(location);
                    CacheUtils.getInstance().put(AMAP_LOCATION, location);
                } else {
                    // 定位失败
                    currentCity = "定位失败";
                    tvCity.setText(currentCity);
                }
            }
        }
    });
    mLocationClient.startLocation();
}
Also used : AMapLocationClient(com.amap.api.location.AMapLocationClient) AMapLocation(com.amap.api.location.AMapLocation) AMapLocationClientOption(com.amap.api.location.AMapLocationClientOption) AMapLocationListener(com.amap.api.location.AMapLocationListener)

Example 3 with AMapLocationListener

use of com.amap.api.location.AMapLocationListener in project KL2 by jweihao.

the class GaoDeActivity method initListener.

private void initListener() {
    // 监测地图画面的移动
    mAMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {

        @Override
        public void onCameraChangeFinish(CameraPosition cameraPosition) {
            if (null != location && null != cameraPosition && isSearchData) {
                mIvLocation.setImageResource(R.mipmap.location_gps_black);
                zoom = cameraPosition.zoom;
                if (null != mSelectByListMarker) {
                    mSelectByListMarker.setVisible(false);
                }
                getAddressInfoByLatLong(cameraPosition.target.latitude, cameraPosition.target.longitude);
                startTransAnimator();
            // doSearchQuery(true, "", location.getCity(), new LatLonPoint(cameraPosition.target.latitude, cameraPosition.target.longitude));
            }
            if (!isSearchData) {
                isSearchData = true;
            }
        }

        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
        }
    });
    // 设置触摸地图监听器
    mAMap.setOnMapClickListener(new AMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latLng) {
            isSearchData = true;
        }
    });
    // Poi搜索监听器
    mOnPoiSearchListener = new onPoiSearchLintener();
    // 逆地址搜索监听器
    mOnGeocodeSearchListener = new GeocodeSearch.OnGeocodeSearchListener() {

        @Override
        public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
            if (i == 1000) {
                if (regeocodeResult != null) {
                    userSelectPoiItem = DataConversionUtils.changeToPoiItem(regeocodeResult);
                    if (null != mList) {
                        mList.clear();
                    }
                    mList.addAll(regeocodeResult.getRegeocodeAddress().getPois());
                    if (null != userSelectPoiItem) {
                        mList.add(0, userSelectPoiItem);
                    }
                    mAddressAdapter.setList(mList);
                    mRecyclerView.smoothScrollToPosition(0);
                }
            }
        }

        @Override
        public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        }
    };
    // gps定位监听器
    mAMapLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation loc) {
            try {
                if (null != loc) {
                    stopLocation();
                    if (loc.getErrorCode() == 0) {
                        // 可在其中解析amapLocation获取相应内容。
                        location = loc;
                        SPUtils.putString(GaoDeActivity.this, DatasKey.LOCATION_INFO, gson.toJson(location));
                        doWhenLocationSucess();
                    } else {
                        // 定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                        Log.e("AmapError", "location Error, ErrCode:" + loc.getErrorCode() + ", errInfo:" + loc.getErrorInfo());
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
    // recycleview列表监听器
    mAddressAdapter.setOnItemClickLisenter(new OnItemClickLisenter() {

        @Override
        public void onItemClick(int position) {
            try {
                isSearchData = false;
                mIvLocation.setImageResource(R.mipmap.location_gps_black);
                moveMapCamera(mList.get(position).getLatLonPoint().getLatitude(), mList.get(position).getLatLonPoint().getLongitude());
                refleshSelectByListMark(mList.get(position).getLatLonPoint().getLatitude(), mList.get(position).getLatLonPoint().getLongitude());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    // 控件点击监听器
    mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            switch(view.getId()) {
                case R.id.iv_back:
                    finish();
                    break;
                case R.id.iv_search:
                    // Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                    startActivityForResult(new Intent(GaoDeActivity.this, SearchActivity.class), SEARCHREQUESTCODE);
                    break;
                case R.id.iv_location:
                    // Toast.makeText(MainActivity.this, "定位", Toast.LENGTH_SHORT).show();
                    mIvLocation.setImageResource(R.mipmap.location_gps_green);
                    if (null != mSelectByListMarker) {
                        mSelectByListMarker.setVisible(false);
                    }
                    if (null == location) {
                        startLocation();
                    } else {
                        doWhenLocationSucess();
                    }
                    break;
                case R.id.bt_send:
                    if (null != mList && 0 < mList.size() && null != mAddressAdapter) {
                        int position = mAddressAdapter.getSelectPositon();
                        if (position < 0) {
                            position = 0;
                        } else if (position > mList.size()) {
                            position = mList.size();
                        }
                        PoiItem poiItem = mList.get(position);
                        Toast.makeText(GaoDeActivity.this, "发送:" + poiItem.getTitle() + "  " + poiItem.getSnippet() + "  " + "纬度:" + poiItem.getLatLonPoint().getLatitude() + "  " + "经度:" + poiItem.getLatLonPoint().getLongitude(), Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;
            }
        }
    };
    mIvBack.setOnClickListener(mOnClickListener);
    mIvSearch.setOnClickListener(mOnClickListener);
    mIvLocation.setOnClickListener(mOnClickListener);
    mBtSend.setOnClickListener(mOnClickListener);
}
Also used : AMapLocation(com.amap.api.location.AMapLocation) AMap(com.amap.api.maps2d.AMap) OnItemClickLisenter(com.jweihao.jdemo.utils.OnItemClickLisenter) AMapLocationListener(com.amap.api.location.AMapLocationListener) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) MapView(com.amap.api.maps2d.MapView) LatLonPoint(com.amap.api.services.core.LatLonPoint) CameraPosition(com.amap.api.maps2d.model.CameraPosition) GeocodeSearch(com.amap.api.services.geocoder.GeocodeSearch) RegeocodeResult(com.amap.api.services.geocoder.RegeocodeResult) PoiItem(com.amap.api.services.core.PoiItem) LatLng(com.amap.api.maps2d.model.LatLng) GeocodeResult(com.amap.api.services.geocoder.GeocodeResult)

Example 4 with AMapLocationListener

use of com.amap.api.location.AMapLocationListener in project CustomViews by AndroidStudy233.

the class MainActivity method initLocation.

private void initLocation() {
    /*
            注意这只是个测试,没有做6.0系统的兼容.6.0以下是没问题的
         */
    mLocationClient = new AMapLocationClient(this);
    AMapLocationClientOption option = new AMapLocationClientOption();
    option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    // 设置只定位一次
    option.setOnceLocation(true);
    // 设置返回地址信息
    option.setNeedAddress(true);
    // option.setInterval(20000);// 设置定位间隔,默认为2000ms,最低1000ms。
    mLocationClient.setLocationListener(new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    String city = aMapLocation.getCity();
                    String district = aMapLocation.getDistrict();
                    MyLogUtil.debug("高德city: " + city);
                    MyLogUtil.debug("高德district: " + district);
                } else {
                    // 定位失败
                    MyLogUtil.error("高德Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
                }
            }
        }
    });
    mLocationClient.setLocationOption(option);
    mLocationClient.startLocation();
}
Also used : AMapLocationClient(com.amap.api.location.AMapLocationClient) AMapLocation(com.amap.api.location.AMapLocation) AMapLocationClientOption(com.amap.api.location.AMapLocationClientOption) AMapLocationListener(com.amap.api.location.AMapLocationListener)

Example 5 with AMapLocationListener

use of com.amap.api.location.AMapLocationListener in project CustomViews by AndroidStudy233.

the class CitySelectorActivity method initGaodeLocation.

/**
 * 初始化高德地图定位
 */
private void initGaodeLocation() {
    gaodeClient = new AMapLocationClient(this);
    AMapLocationClientOption option = new AMapLocationClientOption();
    option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    // 设置只定位一次
    option.setOnceLocation(true);
    // 设置返回地址信息
    option.setNeedAddress(true);
    // 定位超时
    option.setHttpTimeOut(20000);
    // option.setInterval(20000);// 设置定位间隔,默认为2000ms,最低1000ms。
    gaodeClient.setLocationListener(new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    String city = aMapLocation.getCity();
                    String district = aMapLocation.getDistrict();
                    mCityAdapter.updateLocateState(LocateState.SUCCESS, city);
                    CLogUtil.d("高德city: " + city);
                    CLogUtil.d("高德district: " + district);
                } else {
                    // 定位失败
                    mCityAdapter.updateLocateState(LocateState.FAILED, null);
                    CLogUtil.e("高德Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
                }
            }
        }
    });
    gaodeClient.setLocationOption(option);
    // gaodeClient.startLocation();
    checkLocationPermission();
}
Also used : AMapLocationClient(com.amap.api.location.AMapLocationClient) AMapLocation(com.amap.api.location.AMapLocation) AMapLocationClientOption(com.amap.api.location.AMapLocationClientOption) AMapLocationListener(com.amap.api.location.AMapLocationListener)

Aggregations

AMapLocation (com.amap.api.location.AMapLocation)5 AMapLocationListener (com.amap.api.location.AMapLocationListener)5 AMapLocationClient (com.amap.api.location.AMapLocationClient)4 AMapLocationClientOption (com.amap.api.location.AMapLocationClientOption)4 Intent (android.content.Intent)1 Message (android.os.Message)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 AMap (com.amap.api.maps2d.AMap)1 MapView (com.amap.api.maps2d.MapView)1 CameraPosition (com.amap.api.maps2d.model.CameraPosition)1 LatLng (com.amap.api.maps2d.model.LatLng)1 LatLonPoint (com.amap.api.services.core.LatLonPoint)1 PoiItem (com.amap.api.services.core.PoiItem)1 GeocodeResult (com.amap.api.services.geocoder.GeocodeResult)1 GeocodeSearch (com.amap.api.services.geocoder.GeocodeSearch)1 RegeocodeResult (com.amap.api.services.geocoder.RegeocodeResult)1 OnItemClickLisenter (com.jweihao.jdemo.utils.OnItemClickLisenter)1