高德 地图 Marker 点击、滑动 、搜索

mac2025-04-27  6

public class ShopAddressActivity extends BaseActivity { @BindView(R.id.titleBar) MyTitleBar titleBar; @BindView(R.id.mapView) MapView mapView; AMap aMap; @BindView(R.id.et_search_address) EditText etSearchAddress; @BindView(R.id.tv_shop_address_title) TextView tvShopAddressTitle; @BindView(R.id.tv_shop_address_content) TextView tvShopAddressContent; @BindView(R.id.tv_shop_address_ok) TextView tvShopAddressOk; @BindView(R.id.layout_search_address) LinearLayout layoutSearchAddress; private MarkerOptions mMarkerOption; private Marker marker; private MarkerOptions mLoactionMarkerOption; private Marker mLoactioarker; private String keyWord = "";// 要输入的poi搜索关键字 private PoiResult poiResult; // poi返回的结果 private int currentPage = 0;// 当前页面,从0开始计数 private PoiSearch.Query query;// Poi查询条件类 private PoiSearch poiSearch;// POI搜索 private float zoom = 15; private EditText etSearch_address; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明mLocationOption对象 public AMapLocationClientOption mLocationOption = null; private double mLat; private double mLng; private Intent intent; private GeocodeSearch geocodeSearch; private ShopLocationAddressAdapter mShopLocationAddressAdapter; private SupportPopupWindow mPopupWindow; private RecyclerView mPopupRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_shop_address); ButterKnife.bind(this); super.onCreate(savedInstanceState); mapView.onCreate(savedInstanceState);//必须要写 } @Override protected void getExtras() { intent = getIntent(); mLat = intent.getDoubleExtra("lat", 0); mLng = intent.getDoubleExtra("lng", 0); Log.e("intentLat: ", mLat + ""); Log.e("intentLon: ", mLng + ""); } @Override protected void initData() { if (aMap == null) { aMap = mapView.getMap(); } //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); // 绑定marker拖拽事件 aMap.setOnCameraChangeListener(onCameraChangeListener); //地理搜索类 geocodeSearch = new GeocodeSearch(this); geocodeSearch.setOnGeocodeSearchListener(geocodeSearchListener); autoEdit(); setUpMap(); } @Override protected void setListener() { titleBar.setLeftLayoutClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); tvShopAddressOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); Log.e("tvShopAddressOkLat: ", mLat + ""); Log.e("tvShopAddressOkLon: ", mLng + ""); intent.putExtra("lat", mLat); intent.putExtra("lng", mLng); intent.putExtra("address", tvShopAddressContent.getText().toString().substring(9)); setResult(102, intent); finish(); } }); aMap.setOnMapClickListener(new AMap.OnMapClickListener() { @Override public void onMapClick(LatLng latLng) { mLat = latLng.latitude; mLng = latLng.longitude; aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom)); } }); } private void autoEdit() { etSearchAddress.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (!StringUtils.isEmpty(editable.toString().trim())) { // setAddressPopup(); //textChangeSearch(etSearchAddress.toString().trim()); setAddressPopup(); layoutSearchAddress.setVisibility(View.GONE); // etSearchAddress.setSelection(etSearchAddress.getText().length()); } } }); } /** * 搜索 位置 */ private void setAddressPopup() { if (mPopupWindow != null) { return; } View popView = LayoutInflater.from(this).inflate(R.layout.popup_shop_search_address, null); mPopupWindow = new SupportPopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); mPopupRecyclerView = popView.findViewById(R.id.recyclerView); mPopupRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mShopLocationAddressAdapter = new ShopLocationAddressAdapter(R.layout.item_popup_shop_search_address, null); mPopupRecyclerView.setAdapter(mShopLocationAddressAdapter); mPopupWindow.setContentView(popView); mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); mPopupWindow.setFocusable(true); mPopupWindow.showAsDropDown(titleBar.getLeftLayout(), Gravity.BOTTOM, ConvertUtils.dp2px(20)); mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { layoutSearchAddress.setVisibility(View.VISIBLE); mPopupWindow = null; } }); View view_bg = popView.findViewById(R.id.view_popup_bg); etSearch_address = popView.findViewById(R.id.et_popup_search_address); etSearch_address.setText(etSearchAddress.getText().toString()); etSearch_address.setSelection(etSearch_address.getText().length()); mPopupRecyclerView = popView.findViewById(R.id.recyclerView); mPopupRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mShopLocationAddressAdapter = new ShopLocationAddressAdapter(R.layout.item_popup_shop_search_address, null); mPopupRecyclerView.setAdapter(mShopLocationAddressAdapter); view_bg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPopupWindow.dismiss(); } }); etSearch_address.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { etSearchAddress.setText(s.toString()); etSearchAddress.setSelection(etSearchAddress.getText().length()); if (!StringUtils.isEmpty(etSearchAddress.getText().toString())) { doSearchQuery(etSearchAddress.getText().toString().trim()); } else { if (mPopupWindow != null) { mPopupWindow.dismiss(); } } } }); mShopLocationAddressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { mLat = Double.parseDouble(mShopLocationAddressAdapter.getItem(position).getLat()); mLng = Double.parseDouble(mShopLocationAddressAdapter.getItem(position).getLon()); LatLng latLng = new LatLng(mLat, mLng); marker.setPosition(latLng); //CameraUpdate camera = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 19, 0, 0)); aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLat, mLng), zoom)); // tvShopAddressTitle.setText(mShopLocationAddressAdapter.getItem(position).getTitle()); // tvShopAddressContent.setText(mShopLocationAddressAdapter.getItem(position).getContent()); if (mPopupWindow != null) { mPopupWindow.dismiss(); } KeyboardUtils.hideSoftInput(ShopAddressActivity.this); } }); doSearchQuery(etSearchAddress.getText().toString().trim()); } /** * 开始进行poi搜索 */ protected void doSearchQuery(String key) { currentPage = 0; //不输入城市名称有些地方搜索不到 // 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国) query = new PoiSearch.Query(key, "", ""); // 设置每页最多返回多少条poiitem query.setPageSize(10); // 设置查询页码 query.setPageNum(currentPage); //构造 PoiSearch 对象,并设置监听 poiSearch = new PoiSearch(this, query); poiSearch.setOnPoiSearchListener(onPoiSearchListener); //调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。 poiSearch.searchPOIAsyn(); } /** * 声明定位回调监听器 */ public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //定位成功回调信息,设置相关消息 amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表 amapLocation.getLatitude();//获取纬度 amapLocation.getLongitude();//获取经度 amapLocation.getAccuracy();//获取精度信息 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(amapLocation.getTime()); df.format(date);//定位时间 amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。 amapLocation.getCountry();//国家信息 amapLocation.getProvince();//省信息 amapLocation.getCity();//城市信息 amapLocation.getDistrict();//城区信息 amapLocation.getStreet();//街道信息 amapLocation.getStreetNum();//街道门牌号信息 amapLocation.getCityCode();//城市编码 amapLocation.getAdCode();//地区编码 amapLocation.getAoiName();//获取当前定位点的AOI信息 double lat = amapLocation.getLatitude(); double lon = amapLocation.getLongitude(); // 设置当前地图显示为当前位置 if (mLoactionMarkerOption == null) { BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.add_my)); mLoactionMarkerOption = new MarkerOptions() .icon(bitmapDescriptor) .position(new LatLng(lat, lon)) .draggable(true); mLoactioarker = aMap.addMarker(mLoactionMarkerOption); tvShopAddressTitle.setText(amapLocation.getAoiName()); tvShopAddressContent.setText(amapLocation.getAddress()); if (mLat > 0 && mLng > 0) { aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLat, mLng), zoom)); } else { aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), zoom)); } } else { mLoactioarker.setPosition(new LatLng(lat, lon)); } // mLocationClient.stopLocation(); } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } } }; // 搜索 PoiSearch.OnPoiSearchListener onPoiSearchListener = new PoiSearch.OnPoiSearchListener() { @Override public void onPoiSearched(PoiResult result, int rCode) { //rCode 为1000 时成功,其他为失败 if (rCode == AMapException.CODE_AMAP_SUCCESS) { // 解析result 获取搜索poi的结果 if (result != null && result.getQuery() != null) { if (result.getQuery().equals(query)) { // 是否是同一条 poiResult = result; ArrayList<LocationAddressInfo> data = new ArrayList<>();//自己创建的数据集合 // 取得第一页的poiitem数据,页数从数字0开始 //poiResult.getPois()可以获取到PoiItem列表 result.getQuery().setCityLimit(true); List<PoiItem> poiItems = poiResult.getPois(); //若当前城市查询不到所需POI信息,可以通过result.getSearchSuggestionCitys()获取当前Poi搜索的建议城市 List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys(); //如果搜索关键字明显为误输入,则可通过result.getSearchSuggestionKeywords()方法得到搜索关键词建议。 List<String> suggestionKeywords = poiResult.getSearchSuggestionKeywords(); //解析获取到的PoiItem列表 for (PoiItem item : poiItems) { //获取经纬度对象 LatLonPoint llp = item.getLatLonPoint(); double lon = llp.getLongitude(); double lat = llp.getLatitude(); //返回POI的名称 String title = item.getTitle(); //返回POI的地址 String text = item.getSnippet(); data.add(new LocationAddressInfo(String.valueOf(lon), String.valueOf(lat), title, text)); Log.e("onPoiSearched: ", title); } mShopLocationAddressAdapter.setNewData(data); if (data.size() == 0) { setEmptyLayout(); } } } else { ToastUtils.showShort("无搜索结果"); } } else { ToastUtils.showShort("错误码" + rCode); } } @Override public void onPoiItemSearched(PoiItem poiItem, int i) { } }; /** * 设置空布局 */ private void setEmptyLayout() { View view = RelativeLayout.inflate(ShopAddressActivity.this, R.layout.empty_layout, null); TextView textView = view.findViewById(R.id.tv_dataEmpty); textView.setText("暂无数据"); ImageView imageView = view.findViewById(R.id.img_dataEmpty); imageView.setBackgroundResource(R.mipmap.search_pic_empty); mShopLocationAddressAdapter.setEmptyView(view); } AMap.OnCameraChangeListener onCameraChangeListener = new AMap.OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition cameraPosition) { // 添加当前坐标覆盖物 LatLng latLng = new LatLng(mLat, mLng); if (mMarkerOption == null) { BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.shop_address)); mMarkerOption = new MarkerOptions() .icon(bitmapDescriptor) .position(cameraPosition.target) .draggable(true); marker = aMap.addMarker(mMarkerOption); } else { marker.setPosition(cameraPosition.target); } } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { // 添加当前坐标覆盖物 LatLng mapCenterPoint = getMapCenterPoint(); mLat = mapCenterPoint.latitude; mLng = mapCenterPoint.longitude; LatLng latLng = new LatLng(mLat, mLng); getAddressByLatlng(latLng); zoom = cameraPosition.zoom; } }; /** * 配置定位参数 */ private void setUpMap() { //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否只定位一次,默认为false mLocationOption.setOnceLocation(false); //设置是否强制刷新WIFI,默认为强制刷新 mLocationOption.setWifiActiveScan(true); //设置是否允许模拟位置,默认为false,不允许模拟位置 mLocationOption.setMockEnable(false); //设置定位间隔,单位毫秒,默认为2000ms mLocationOption.setInterval(2000); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 aMap.getUiSettings().setZoomControlsEnabled(false); //启动定位 mLocationClient.startLocation(); } /** * by moos on 2017/09/05 * func:获取屏幕中心的经纬度坐标 * * @return */ public LatLng getMapCenterPoint() { int left = mapView.getLeft(); int top = mapView.getTop(); int right = mapView.getRight(); int bottom = mapView.getBottom(); // 获得屏幕点击的位置 int x = (int) (mapView.getX() + (right - left) / 2); int y = (int) (mapView.getY() + (bottom - top) / 2); Projection projection = aMap.getProjection(); LatLng pt = projection.fromScreenLocation(new Point(x, y)); return pt; } private void getAddressByLatlng(LatLng latLng) { //逆地理编码查询条件:逆地理编码查询的地理坐标点、查询范围、坐标类型。 LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude); RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200f, GeocodeSearch.AMAP); //异步查询 geocodeSearch.getFromLocationAsyn(query); } //得到逆地理编码异步查询结果 GeocodeSearch.OnGeocodeSearchListener geocodeSearchListener = new GeocodeSearch.OnGeocodeSearchListener() { @Override public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) { RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress(); String formatAddress = regeocodeAddress.getFormatAddress(); tvShopAddressTitle.setText(regeocodeAddress.getPois().get(0).getTitle()); tvShopAddressContent.setText(formatAddress); Log.e("onRegeocodeSearched: ", "查询经纬度对应详细地址:\n" + formatAddress); } @Override public void onGeocodeSearched(GeocodeResult geocodeResult, int i) { } }; /** * 方法必须重写 */ @Override protected void onResume() { super.onResume(); if (mapView != null) { mapView.onResume(); } } /** * 方法必须重写 */ @Override protected void onPause() { super.onPause(); if (mapView != null) { mapView.onPause(); } } @Override protected void onStop() { super.onStop(); mLocationClient.stopLocation();//停止定位 } /** * 方法必须重写 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mapView != null) { mapView.onSaveInstanceState(outState); } } /** * 方法必须重写 */ @Override protected void onDestroy() { super.onDestroy(); if (mapView != null) { mapView.onDestroy(); } mLocationClient.onDestroy();//销毁定位客户端。 }

}

最新回复(0)