【问题标题】:Assign a click listener to the info window, cannot navigate to the webpage Google Maps V2为信息窗口分配点击侦听器,无法导航到 Google Maps V2 网页
【发布时间】:2015-05-31 21:55:36
【问题描述】:

问题是我无法单击标记的自定义信息窗口内的 andoid 地图 v2 上显示的 URL“www.news.com”。 在 xml 布局文件中,我有:

<TextView
    android:id="@+id/web"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:background="#ADD8E6"
    android:text="www.google.gr"
    android:linksClickable="true"/>

在 Java 活动文件中我有:

 mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

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

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

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

                TextView tvTitle = (TextView) v.findViewById(R.id.title);
                tvTitle.setText(marker.getTitle());
                TextView tvSnippet = (TextView) v.findViewById(R.id.snippet);
                tvSnippet.setText(marker.getSnippet());

               // URL
               final TextView tvURL =(TextView) v.findViewById(R.id.web);
               tvURL.setText("www.news.com");
               Linkify.addLinks(tvURL, Linkify.WEB_URLS);
                // URL

链接以蓝色和下划线正确显示,但我发现它只是一个死链接。如果我们尝试点击它,什么都不会发生。我必须做什么才能使链接处于活动状态?

在 java 文件中插入新代码:

 mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

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

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

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

            TextView tvTitle = (TextView) v.findViewById(R.id.title);
            tvTitle.setText(marker.getTitle());
            TextView tvSnippet = (TextView) v.findViewById(R.id.snippet);
            tvSnippet.setText(marker.getSnippet());

           // URL

                final TextView tvWeb =(TextView) v.findViewById(R.id.web);
                tvWeb.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.news.com"));
                        startActivity(viewIntent);
                    }
                });

XML 使用相同:

    <TextView
    android:id="@+id/web"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:background="#ADD8E6"
    android:text="www.news.com"
    android:linksClickable="true"/>

问题仍然存在..URL 链接无效,我无法打开浏览器。

当我点击自定义信息窗口时,现在 URL 链接正在打开到移动浏览器。

 mMap.addMarker(new MarkerOptions().position(new LatLng(39.686286, 19.838443)).title("Hi").snippet("Name, Surname, phone: 1093595, website: www.news.com").icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.news.com"));
            startActivity(viewIntent);


        }
    });

但我有大约 300 个标记,我希望为每个标记打开一个单独的 URL 链接。我该如何实施?

更新代码:

mMap.addMarker(new MarkerOptions().position(new LatLng(36.84449, 22.99999)).title("KALOGRIA").snippet("Name: Messinia \nSurname: Stoupa \nphone: +99999999 \nwebsite: http://www.news.com/").icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));


mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener()
    {
    @Override
    public void onInfoWindowClick(Marker marker)
    {
        //String webUrl = marker.getSnippet();
        String snippetStr = marker.getSnippet();
        String webUrl = snippetStr.substring(snippetStr.lastIndexOf("website:") + 1);
        Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
        startActivity(viewIntent);
    }
    });

【问题讨论】:

标签: android google-maps-markers google-maps-api-2


【解决方案1】:

信息窗口不是实时视图,而是将视图呈现为地图上的图像。所以视图只响应点击作为一个整体视图。对于您的情况,我想您必须使用GoogleMap.setOnInfoWindowClickListener,然后检索要启动的网络字符串。


[更新1]

也许你可以试试这个方法:

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener()
{

           @Override
           public void onInfoWindowClick(Marker thisMarker)
           {

                Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.news.com"));
                startActivity(viewIntent);                 

           }

});


[UPDATE2]

如果您有多个Markers,并且想要在单击每个Marker 时打开不同的Activity,那么一种解决方案是将Activity 信息放入MarkerSnippet 为:

Marker marker = mMap.addMarker(new MarkerOptions()
 .position(...)
 .title(...)
 .snippet("http://www.news.com"); //put your activity info here

然后点击Marker时获取:

@Override
public void onInfoWindowClick(Marker thisMarker)
{
         ... ...
     String snippetStr = thisMarker.getSnippet();
     String webUrl = snippetStr.substring(snippetStr.lastIndexOf("website:") + 1);
     Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
     startActivity(viewIntent);
}

【讨论】:

  • @Thanos,你的问题怎么样了?
  • 我的链接仍然有问题。它不可点击。请检查我上面的新代码,请帮我解决这个问题。
  • 新代码在上面的“Java 文件中插入的新代码:”下
  • 只是 UPDATE 答案,我不确定这是最佳解决方案,但可以尝试一下。无论如何,这应该会启动您的网络。
  • 是的,它工作正常。谢谢你。现在我正在尝试为每个标记单独打开不同的活动。你知道怎么做吗?或者我能看到的任何例子?请上面的代码。
猜你喜欢
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 2013-01-09
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多