我在我的应用程序中使用谷歌地图,当我再次打开标记信息窗口时,我想点击信息窗口布局,但它不适用于布局。如果我给按钮单击监听器意味着它的工作。请帮我解决这个问题。
的活动:
public class SampleFragment1 extends Fragment implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {
private ViewGroup infoWindow;
private TextView infoTitle;
private TextView infoSnippet,userNameTV,loggedLabel,userLastNameTV,time,distance,tasks;
private Button infoButton, infoButton2;
private OnInfoWindowElemTouchListener infoButtonListener;
LinearLayout markerlay;
GoogleMap mMap;
public void createLocationRequest() {
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
private MapView mapView;
private MarkerOptions mMarker;
MapWrapperLayout mapWrapperLayout;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.con_fragment_google, container, false);
mapWrapperLayout = (MapWrapperLayout)view.findViewById(R.id.map_relative_layout);
mapView = (MapView)view. findViewById(R.id.mapView);
// getContext().startService(new Intent(getContext(),GPSTracker.class));
mapView.onCreate(savedInstanceState);
buildGoogleApiClient();
createLocationRequest();
mapView.getMapAsync(this);
return view;
}
private synchronized void buildGoogleApiClient() {
try {
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
// to get current latitude and longitude
// storing the updated latitde and longitude when user moving and reach destination
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mapWrapperLayout.init(mMap, getPixelsFromDp(getContext(), 39 + 20));
// We want to reuse the info window for all the markers,
// so let's create only one class member instance
this.infoWindow = (ViewGroup)getActivity().getLayoutInflater().inflate(R.layout.info_widow_layout, null);
this.infoTitle = (TextView)infoWindow.findViewById(R.id.nametv);
this.infoSnippet = (TextView)infoWindow.findViewById(R.id.addressTv);
this.infoButton = (Button)infoWindow.findViewById(R.id.clinicType);
// Setting custom OnTouchListener which deals with the pressed state
// so it shows up
this.infoButtonListener = new OnInfoWindowElemTouchListener(infoTitle
) //btn_default_pressed_holo_light
{
@Override
protected void onClickConfirmed(View v, Marker marker) {
// Here we can perform some action triggered after clicking the button
Toast.makeText(getContext(), marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
}
};
this.infoTitle.setOnTouchListener(infoButtonListener);
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
infoTitle.setText(marker.getTitle());
infoSnippet.setText(marker.getSnippet());
infoButtonListener.setMarker(marker);
// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
// Let's add a couple of markers
}
public static int getPixelsFromDp(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
注意:这里我给出了textview的click监听器,即布局中的textview。