ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Google Maps Android的自定义信息窗口

2019-10-12 16:24:47  阅读:300  来源: 互联网

标签:infowindow android google-maps android-button google-maps-markers


我想在单击标记时显示一个自定义信息窗口.

我希望它是这样的:

ImageView

TextView

TextView

TextView

Button  Button

我看到了其他人对此的提问,但是仍然令人困惑.

解决方法:

关于ImageView和TextView,请尝试以下操作:

设置信息窗口布局:infowindowlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

在您的MapActivity中(这是一个标记的示例,但是您也可以将其用于其他标记):

public class MapActivity extends Activity  {
private GoogleMap mMap;
private Marker Somewhere;
private int markerclicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
...
...
final LatLng somewhere = new LatLng(..., ...);

Somewhere=mMap.addMarker(new MarkerOptions()
.position(somewhere)
.title("YOUR TITLE")
.snippet("INFO")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

...
...
...

mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {

            // Getting view from the layout file infowindowlayout.xml
            View v = getLayoutInflater().inflate(R.layout.infowindowlayout, null);

            LatLng latLng = arg0.getPosition();

            ImageView im = (ImageView) v.findViewById(R.id.imageView1);
            TextView tv1 = (TextView) v.findViewById(R.id.textView1);
            TextView tv2 = (TextView) v.findViewById(R.id.textView2);
            String title=arg0.getTitle();
            String informations=arg0.getSnippet();

            tv1.setText(title);
            tv2.setText(informations);

            if(onMarkerClick(arg0)==true && markerclicked==1){
               im.setImageResource(R.drawable.yourdrawable);
              }


            return v;

        }
    });
}

public boolean onMarkerClick(final Marker marker) {

if (marker.equals(Somewhere)) 
  {
      markerclicked=1;
      return true;
  }
return false; 
}

关于按钮,图像按钮等,是没有办法的. documentation说:

As mentioned in the previous section on info windows, an info window is not a live View, rather the view is rendered as an image onto the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window.

无论如何,似乎有解决方案,请参见this answer here.我不知道它是否适用于我的代码,但似乎可以使用按钮和图像按钮.

标签:infowindow,android,google-maps,android-button,google-maps-markers
来源: https://codeday.me/bug/20191012/1901624.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有