百度地图 标注 显示信息 Marker单击事件 代码参考

时间:2022-07-21 22:34:41



package com.renhui.android.project_baidumap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity implements OnGetPoiSearchResultListener
, BaiduMap.OnMarkerClickListener, OnGetGeoCoderResultListener{
private static final String TAG = "APP_Main2Activity";
private MapView mMapView;
private EditText edit_city, edit_content;
private BaiduMap mBaiduMap;
private PoiSearch mPoiSearch;
private GeoCoder mGeoCoder;
private TextView textView;
private RelativeLayout mRelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = (TextView) findViewById(R.id.textView);
textView.setVisibility(View.GONE);
edit_content = (EditText) findViewById(R.id.edit_content);
edit_city = (EditText) findViewById(R.id.edit_city);
mMapView = (MapView) findViewById(R.id.bmapView1);
mBaiduMap = mMapView.getMap();

mRelativeLayout = (RelativeLayout) findViewById(R.id.layout);

mGeoCoder = GeoCoder.newInstance();
mGeoCoder.setOnGetGeoCodeResultListener(this);

mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(this);
mBaiduMap.setOnMarkerClickListener(this);
}

public void findCityClick(View view) {
String city = edit_city.getText().toString();
String content = edit_content.getText().toString();
Log.d(TAG, "findCityClick: city = " + city);
Log.d(TAG, "findCityClick: content = " + content);
if (TextUtils.isEmpty(city)) {
city = "北京";
}
mPoiSearch.searchInCity(new PoiCitySearchOption()
.city(city)
.keyword(content)
.pageNum(1)
.pageCapacity(10));
Toast.makeText(Main2Activity.this, "正在检索", Toast.LENGTH_SHORT).show();
//// }
// mGeoCoder.geocode(new GeoCodeOption().city(city).address(content));
}

@Override
public void onGetPoiResult(PoiResult poiResult) {
Log.d(TAG, "onGetPoiResult: poiResult" + poiResult);
mBaiduMap.clear();
List<PoiInfo> poiInfo = poiResult.getAllPoi();
ArrayList<OverlayOptions> over = new ArrayList<>();
for (PoiInfo info : poiInfo) {
textView.setText(info.name);
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(textView);
OverlayOptions overlay = new MarkerOptions()
.title( "名称:"+info.name + "\n" +"地址:"+ info.address+"\n"+ "电话:"+ info.phoneNum)
.position(info.location)
.icon(bitmap);
over.add(overlay);
}
mBaiduMap.addOverlays(over);
}

@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {}

/**
* Marker的点击事件
* @param marker
* @return
*/
@Override
public boolean onMarkerClick(Marker marker) {
String titleName = marker.getTitle();
LatLng position = marker.getPosition();
Toast.makeText(Main2Activity.this,titleName, Toast.LENGTH_SHORT).show();
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(position);
mBaiduMap.setMapStatus(mapStatusUpdate);

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/2;
int width = displayMetrics.widthPixels/2;
createPopupWindow(titleName).showAtLocation(mRelativeLayout, Gravity.TOP,height,width);
return true;
}
/**
* 创建地理编码检索监听者
* @param geoCodeResult
*/
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
String name = geoCodeResult.getAddress();
Log.d(TAG, "onGetGeoCodeResult: name =" + name);
LatLng location = geoCodeResult.getLocation();
Log.d(TAG, "onGetGeoCodeResult: location =" + location);
mPoiSearch.searchNearby(new PoiNearbySearchOption()
.location(location)
.keyword(name)
.radius(2000));
// MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(location);
//某处坐标 40.1077840000,116.3894130000
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {}

/**
* 创建一个 PopupWindow 弹窗
*/
private PopupWindow createPopupWindow(String title){

View view = getLayoutInflater().inflate(R.layout.item_popu,null);
PopupWindow popupWindow = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//设置动画效果
popupWindow.setAnimationStyle(android.R.anim.fade_in);
// //设置背景
// popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.meinv1));
//设置透明度
// popupWindow.getBackground().setAlpha(100);
TextView tv_info = (TextView) view.findViewById(R.id.text_info);
tv_info.setText(title);
//获取焦点
popupWindow.setFocusable(true);
//可以触摸
popupWindow.setTouchable(true);
//
popupWindow.setOutsideTouchable(true);
//防止虚拟软键盘被弹出菜单遮住
popupWindow.setInputMethodMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

// popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
return popupWindow; //返回一个popupWindow对象,那么直接调用该方法就可以了
}
}