Как сделать свое окошко маркера для гуглокарт на андроид-устройстве
Стандартный снипет маркера на виджете MapView довольно удобен для небольших надписей. Однако же, если хочется сделать,к примеру, многострочный текст или же использовать какой-то дополнительный функционал на самом окошке снипета, то нам придется реализовывать функционал своими руками.
Нам нужен класс, который реализует интерфейс
1 |
implements GoogleMap.InfoWindowAdapter |
И в нем мы определяем два метода getInfoWindow и getInfoContent. Первый определяет внешний вид всего окна, но нам заморачиваться с прорисовкой тенюшек, указателей и прочего совсем ни к чему, поэтому мы вернем в этом методе null
1 2 3 4 |
Override public View getInfoWindow(Marker marker) { return null; } |
А вот содержимое мы изменим. Загрузим данный из Layout-а и нарисуем свое окошко
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override public View getInfoContents(Marker marker) { LayoutInflater inflater = LayoutInflater.from(this); View page = inflater.inflate(R.layout.map_description, null); TextView title = (TextView)page.findViewById(R.id.map_label_title); TextView description = (TextView)page.findViewById(R.id.map_label_description); title.setText(marker.getTitle()); description.setText(marker.getSnippet()); return page; } |
И файл map_description.xml из папки layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="@string/hello_world" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/map_label_title" android:textStyle="bold" /> <TextView android:text="@string/hello_world" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/map_label_description" /> </LinearLayout> </LinearLayout> |
И установим для нашего маркера этот класс-обработчик вызовом метода
1 2 3 4 |
GoogleMap map = mMapView.getMap(); if (map != null) { map.setInfoWindowAdapter(this); } |
Как создать создать Google Map View и использовать его в своем приложении подробно описано в предыдущей статье.