How to make a link clickable in a Google Map Marke

2019-09-06 15:54发布

问题:

I'm displaying a couple of markers on a Google Map. When I click on a marker it opens a custom InfoWindowAdapter

    this.map.setInfoWindowAdapter(new CustomInfoWindowAdapter(getLayoutInflater()));

In this CustomInfoWindowAdapter I display some text and create a link when a phone number is found:

@Override
public View getInfoContents(Marker marker) {
    if (this.popup == null) {
        this.popup = inflater.inflate(R.layout.poi_marker, null);
    }

    TextView tv = (TextView) popup.findViewById(R.id.title);

    tv.setText(marker.getTitle());
    tv = (TextView) popup.findViewById(R.id.snippet);
    tv.setText(marker.getSnippet());
    Linkify.addLinks(tv, Linkify.PHONE_NUMBERS);

    return(popup);
}

The display is then correct. I can see that the phone number is displayed as a link, but I cannot press on it... How I can I make it open the dialer ?

回答1:

I finally managed to do it like that:

Yes but the problem IS the phone parsing ;)

I managed to do it like that

this.map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            final String description = marker.getSnippet();
            if (!TextUtils.isEmpty(description)) {
                final Spannable span = Spannable.Factory.getInstance().newSpannable(description);
                if (Linkify.addLinks(span, Linkify.PHONE_NUMBERS)) {
                    final URLSpan[] old = span.getSpans(0, span.length(), URLSpan.class);
                    if (old != null && old.length > 0) {
                         Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(old[0].getURL()));
                         startActivity(intent);
                    }
                }
            }
        }
    });