美文网首页
在高德地图上添加自定义Marker

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

作者: 留给时光吧 | 来源:发表于2018-05-09 20:07 被阅读0次
        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>
    

    相关文章

      网友评论

          本文标题:在高德地图上添加自定义Marker

          本文链接:https://www.haomeiwen.com/subject/vdxorftx.html