Google Maps API - 地图信息视图中的超链接无法打开原生GoogleMaps

时间:2015-06-20 16:52:05

标签: android google-maps hyperlink google-maps-android-api-2 onclicklistener

我正在使用自定义GoogleMap开发InfoView地图。在我的InfoView中,我想要一个超链接打开本机GoogleMap应用程序,我的经度和纬度在Click上。

enter image description here

我尝试过使用Common Intents API指南并使用以下语法创建超链接:

geo:latitude,longitude

在我的代码中:

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

到我InfoView的{​​{1}}。带下划线的链接正在显示,但是当我点击它时没有任何反应。

  

完整的代码:

TextView

@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

2 个答案:

答案 0 :(得分:1)

看起来无法在InfoWindow中点击任何单个项目。请参阅this answer

另外,在文档here中:

  

正如上一节关于信息窗口中所提到的,一个信息窗口   不是实时视图,而是将视图渲染为图像   地图。因此,您在视图上设置的任何侦听器都将被忽略   并且你无法区分各个部分的点击事件   风景。建议您不要放置交互式组件 - 例如   作为按钮,复选框或文本输入 - 在您的自定义信息中   窗口。

您可以做的是使整个InfoWindow可以OnInfoWindowClickListener点击,例如:

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


        }
    });

答案 1 :(得分:-1)

enter image description here

找到附图像ya kharoof!