1

我正在開發一個帶有自定義InfoViewGoogleMap地圖。在我的InfoView中,我想要一個超鏈接打開本地GoogleMap應用程序,並在Click上輸入我的經度和緯度。Google Maps API - 地圖中的超鏈接InfoView無法打開本地GoogleMaps

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「 s 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

<?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> 
+1

它看起來像你試圖做的是不可能的。你可以做的是設置一個'OnInfoWindowClickListener'看到這個答案︰http://stackoverflow.com/questions/22348488/how-to-open-new-activity-or-dialog-or-link-from-a-infowindow- google-maps-v2/22350071#22350071 –

+0

謝謝你是對的,在這裏也指定https://developers.google.com/maps/documentation/android/infowindows#info_window_events「...因此,任何聽衆你視圖上的設置被忽略,您無法區分視圖各個部分的點擊事件。「 我已經設置了'OnInfoWindowClickListenre',它的工作原理。你應該爲我的問題添加一個答案,以便我接受它 –

回答

1

它看起來是不可能使InfoWindow中的任何單個項目都可點擊。參考this answer

同時,文檔here在:

由於在信息窗口的一節中提到,信息窗口 不是實時取景,而該視圖作爲圖像呈現到 地圖。因此,您在視圖上設置的任何聽衆都會被忽略 ,並且您無法區分視圖各個部分的點擊事件。建議您不要在您的自定義信息窗口中放置交互式組件 - 例如 作爲按鈕,複選框或文本輸入。

你可以做的是使整個信息窗口點擊與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

enter image description here

找到附加的圖像雅kharoof!

相關問題