Assign a click listener to the info window, cannot navigate to the webpage Google Maps V2

不问归期 提交于 2019-12-24 21:32:13

问题


The problem is that I cannot click on the URL "www.news.com" displayed on andoid map v2 inside custom infowindow of a marker. In xml layout file I have:

<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"/>

In Java activity file I have:

 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

The link is showing properly with blue color and underline, but I found the its just a dead link. Nothing is happening if we try to click it. What I have to do to make the link active?

New code inserted inside java file:

 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 used the same:

    <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"/>

Still problem exists..URL link is not active, I cannot open browser.

Now URL link is opening to mobile browser when I click on custom infowindow.

 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);


        }
    });

But I have around 300 markers and for each marker I wish to open a separate URL link. How can I implement this?

UPDATE CODE:

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);
    }
    });

回答1:


Info window is not a live View, rather the view is rendered as an image onto the map. So the view only response click as a whole view. For your case I suppose you have to use GoogleMap.setOnInfoWindowClickListener, and then retrieve the web string to launch.


[UPDATE1]

Maybe you could try this way:

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]

For the case you have several Markers, and want to open different Activity when click each Marker, then one solution is to put Activity info to Marker's Snippet as:

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

and then get it when click 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);
}


来源:https://stackoverflow.com/questions/29302927/assign-a-click-listener-to-the-info-window-cannot-navigate-to-the-webpage-googl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!