Android 高德地图添加自定义Marker,添加图片Marker,Marker点击事件

地图点击事件

 

map.setOnMapClickListener(new AMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        addMarker(map, latLng.latitude, latLng.longitude, latLng.toString());
    }
});

 

private void addMarker(AMap map, double latitude, double longitude, String title) {
    MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(latitude, longitude)).title(title)
            .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.device_mark_icon)));
    map.addMarker(markerOptions);
}

 

 

添加图片Marker图标

BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.qidian));
final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).icon(bitmapDescriptor));

 

1、添加Marker点击事件——根据markerID区分各个Marker的点击事件

mapView = (MapView) findViewById(R.id.map);
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
mapView.onCreate(savedInstanceState);
if (aMap == null) {
    Log.i("lgq","sssssfa====aMap == null");
    aMap = mapView.getMap();
    //设置显示定位按钮 并且可以点击
    UiSettings settings = aMap.getUiSettings();
    aMap.setLocationSource(this);//设置了定位的监听
    // 是否显示定位按钮
    settings.setMyLocationButtonEnabled(true);
    aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase


    aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            Log.i("lgq","dianjiddd===="+marker.getPeriod());//获取markerID
            bottomli.setVisibility(View.VISIBLE);
            bottomte.setText("地址是:"+marker.getPeriod());
            return false;
        }
    });

2、给地图添加自定义Marker

添加多个Marker

setMarker("大象网吧",23.025845,113.752532,1);
setMarker("小猴网吧",23.025845,113.772532,2);
setMarker("大英超网吧",23.024845,113.782532,3);
setMarker("京山市场网吧",23.086634,113.849915,4);
setMarker("东城亿嘉网咖",23.036034,113.816161,5);
public void setMarker(String barname,double wd,double jd,int mid){
    LayoutInflater mInflater = LayoutInflater.from(this);
    View view = mInflater.inflate(R.layout.zdyview, null);
    TextView textView = view.findViewById(R.id.tete);
    textView.setText(barname);

    Bitmap bitmap =convertViewToBitmap(view);

    //绘制marker
    Marker marker2 = aMap.addMarker(new MarkerOptions()
            .position(new LatLng(wd,jd)).period(mid)//添加markerID
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .draggable(false));
}

 

view转bitmap方法
//view 转bitmap

public static Bitmap convertViewToBitmap(View view) {

    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.buildDrawingCache();

    Bitmap bitmap = view.getDrawingCache();

    return bitmap;

}

 zdyview文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:textColor="@color/colorPrimary"
        android:background="@mipmap/custom_info_bubble"
        android:paddingRight="3dp"
        android:paddingLeft="3dp"
        android:textStyle="bold"
        android:text="速度"
       />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/dw64"/>

</LinearLayout>
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
自定义高德地图Marker,可以按照以下步骤操作: 1. 创建MarkerOptions对象,设置Marker的经纬度、图标等属性。 2. 通过AMap类的addMarker方法将Marker添加到地图上。 3. 通过AMap类的setInfoWindowAdapter方法设置Marker的信息窗口。 4. 通过AMap类的setOnMarkerClickListener方法设置Marker点击事件。 具体实现代码如下: ``` // 创建MarkerOptions对象 MarkerOptions markerOptions = new MarkerOptions(); LatLng latLng = new LatLng(39.906901, 116.397972); markerOptions.position(latLng); markerOptions.title("Marker标题"); markerOptions.snippet("Marker描述"); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon)); // 添加Marker到地图上 Marker marker = aMap.addMarker(markerOptions); // 设置Marker的信息窗口 aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() { @Override public View getInfoWindow(Marker marker) { View view = getLayoutInflater().inflate(R.layout.marker_info_window, null); TextView titleTextView = view.findViewById(R.id.title); TextView snippetTextView = view.findViewById(R.id.snippet); titleTextView.setText(marker.getTitle()); snippetTextView.setText(marker.getSnippet()); return view; } @Override public View getInfoContents(Marker marker) { return null; } }); // 设置Marker点击事件 aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); return true; } }); ``` 其中,R.drawable.marker_icon是你自定义Marker图标。marker_info_window是你自定义Marker信息窗口布局。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值