Google Maps API - Hyperlink in Map Infoview not opening native GoogleMaps

309 Views Asked by At

I am developing a GoogleMap Map with a custom InfoView. In my InfoView, I want a hyperlink opening the native GoogleMap application with my longitude and latitude on Click.

enter image description here

I've tried by using the Common Intents API Guide and creating a hyperlink with this syntax:

geo:latitude,longitude

and in my code:

            TextView link = (TextView) v.findViewById(R.id.infoview_link);
            link.setText(Html.fromHtml("<a href='geo:44.492198,11.349071'>"
                            +"Guarda mappa a schermo intero »</a>"));
            link.setClickable(true);
            link.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(
                            getActivity(),"InfoView link Clicked",
                    Toast.LENGTH_LONG).show();
                }
            });

to my InfoView's TextView. The underlined link is showing, but nothing happens when I click on it.

The complete code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {
    final String br = "<br>";
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.details_fragment_two, container, false);

    MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager()
            .findFragmentById(R.id.mapFrag);
    mapFragment.getMapAsync(this);
    gmap = mapFragment.getMap();

    // Setting a custom info window adapter for the google map
    gmap.setInfoWindowAdapter(new GoogleMap.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 info_window_layout
            View v = getLayoutInflater(savedInstanceState).inflate(R.layout.windowlayout, null);

            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();
            String latitude = "<b>Lat: </b>"+latLng.latitude+", ";
            String longitude = "<b>Lon: </b>"+latLng.longitude;

            // Getting reference to the TextView to set latitude
            TextView title = (TextView) v.findViewById(R.id.infoview_title);
            // Getting reference to the TextView to set longitude
            TextView body = (TextView) v.findViewById(R.id.infoview_body);

            TextView link = (TextView) v.findViewById(R.id.infoview_link);
            link.setText(Html.fromHtml("<a href='geo:44.492198,11.349071'>"
                            +"Guarda mappa a schermo intero »</a>"));
            link.setClickable(true);
            link.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(
                            getActivity(),"InfoView link Clicked",
                    Toast.LENGTH_LONG).show();
                }
            });

            title.setText(Html.fromHtml("<b>Piazza Santo Stefano</b>"));

            // Setting the longitude //"Longitude:" + latLng.longitude
            body.setText(Html.fromHtml("Centro di Bologna"+br
                    +latitude + longitude));
            // Returning the view containing InfoWindow contents
            return v;
        }
    });

    LinearLayout separator = (LinearLayout) rootView.findViewById(R.id.map_separator);
        TextView sep = (TextView) separator.findViewById(R.id.separator_header);
        sep.setText("Punto di ritrovo");
    TextView title = (TextView) rootView.findViewById(R.id.mapInfo);
    title.setText(Html.fromHtml("<b>Piazza Santo Stefano</b>"+
            ", davanti alla Basilica di Santo Stefano"));

    return rootView;
}
@Override
public void onMapReady(GoogleMap map) {
           CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(44.492198, 11.349071))
            .zoom(15f)
            .tilt(60) 
            .build(); 

    map.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_markerbyc))
            .position(new LatLng(44.492198, 11.349071)))
            .showInfoWindow();

    UiSettings uiSettings = map.getUiSettings();
        uiSettings.setAllGesturesEnabled(true);
        uiSettings.setZoomControlsEnabled(true);
        uiSettings.setIndoorLevelPickerEnabled(true);
        uiSettings.setCompassEnabled(true);
        uiSettings.setMapToolbarEnabled(true);
        uiSettings.setMyLocationButtonEnabled(true);

    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    map.setMyLocationEnabled(true);
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}

layout/details_fragment_two.xml:

<LinearLayout android:id="@+id/map"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:orientation="vertical"> <!--android:paddingRight="16dp"--> <!--android:paddingLeft="16dp"-->

    <LinearLayout android:layout_height="0dp" android:layout_weight="8"
            android:layout_width="match_parent"
            android:orientation="vertical" android:paddingBottom="1dp"
            android:background="@drawable/image_border">
        <!--android:layout_height="wrap_content"-->
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapFrag"
            android:layout_height="fill_parent" android:layout_width="match_parent"
            android:name="com.google.android.gms.maps.MapFragment" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp" android:layout_weight="2.2"
        android:orientation="vertical"
        android:paddingLeft="16dp" android:paddingRight="16dp"
        android:paddingTop="16dp">

        <include android:id="@+id/map_separator"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginTop="0dp" android:layout_marginBottom="0dp"
            layout="@layout/prenota_separator">
        </include>

        <TextView android:id="@+id/mapInfo"
            android:layout_marginTop="8dp" android:layout_marginBottom="16dp"
            android:gravity='left'
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text='Partenza da: Piazza Santo Stefano'
            android:textSize="@dimen/textSizeSmall" android:textColor="@color/textLightBlack"
            />
    </LinearLayout>

</LinearLayout>

layout/windowlayout.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">

    <TextView
        android:id="@+id/infoview_title" android:textSize="@dimen/textSizeBig"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/infoview_body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/infoview_link" android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/md_orange_700"/>

</LinearLayout>
2

There are 2 best solutions below

0
On BEST ANSWER

It looks like it is not possible to make any individual item in an InfoWindow clickable. Refer to this answer.

Also, in the documentation here:

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.

What you can do is make the entire InfoWindow clickable with OnInfoWindowClickListener, for example:

gmap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            //get location of Marker that was just clicked
            LatLng latLon = marker.getPosition();

            // get the title of Marker that was just clicked
            String title = marker.getTitle();

            //set up your location info based on the Marker info
            //........

           Intent intent = new Intent(getActivity(), MainActivity.class);
           getActivity().startActivity(intent);
           Toast.makeText(
                        getActivity(),"InfoView link Clicked",
                Toast.LENGTH_LONG).show();


        }
    });
0
On

enter image description here

find the attached image ya kharoof!