在高德地图上添加自定义Marker

    public void showCityAtMap(MapView mapView) {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        aMap.clear();
        aMap.moveCamera(CameraUpdateFactory.zoomTo(5));
        ArrayList<WeatherDataInfo> allCity = (ArrayList<WeatherDataInfo>) DataSupport.findAll(WeatherDataInfo.class);
        for (WeatherDataInfo weatherDataInfo : allCity) {
            LatLng latLng = new LatLng(weatherDataInfo.getLat(), weatherDataInfo.getLon());
            aMap.addMarker(new MarkerOptions().position(latLng).draggable(false).snippet(""+weatherDataInfo.getPosition())
                    .icon(BitmapDescriptorFactory.fromView(
                                    getInfoWindow(weatherDataInfo.getName(),
                                            weatherDataInfo.getTmp(),
                                            weatherDataInfo.getIcon()))));
        }
        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                listener.onWindowClick(Integer.parseInt(marker.getSnippet()));
                return true;
            }
        });
    }

主要是利用addMarker方法添加一个Marker,但Marker的样式为自定义的View:

    public View getInfoWindow(String city, String temp, int icon) {
        View view = View.inflate(BaseUtils.getContext(), R.layout.marker_info_layout, null);
        TextView tvCity = (TextView) view.findViewById(R.id.tv_city);
        TextView tvTemp = (TextView) view.findViewById(R.id.tv_temp);
        ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
        tvCity.setText(city);
        tvTemp.setText(temp);
        ivIcon.setImageResource(icon);
        return view;
    }

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/custom_info_bubble"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_city"
            android:textSize="20sp"
            android:textColor="@color/colorAccent"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:gravity="center">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/iv_icon"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_temp"
            android:textColor="@android:color/black"
            android:textSize="26sp"/>
    </LinearLayout>
</LinearLayout>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,272评论 25 708
  • 原文链接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影阅读 33,170评论 6 472
  • 快速排序 平均时间复杂度是O(N*lgN),最坏情况下是O(N^2),是一个不稳定的算法
    zychen143阅读 181评论 0 0
  • 我会等,好的事情总会到来,如果来晚了,就把它当作一个惊喜。
    韩灰灰阅读 213评论 0 1
  • 最美人间四月天,四月,随着指尖轻轻流逝的时光,已渐渐远去。那些开过的花,已被时光收入流年的画卷。风儿轻轻吹过,闭上...
    王者令牌阅读 491评论 0 1

友情链接更多精彩内容