com.google.android.gms.maps.GoogleMap.OnMarkerClickListener Java Examples

The following examples show how to use com.google.android.gms.maps.GoogleMap.OnMarkerClickListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MyTracksMapFragment.java    From mytracks with Apache License 2.0 4 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  if (savedInstanceState != null) {
    keepCurrentLocationVisible = savedInstanceState.getBoolean(
        KEEP_CURRENT_LOCATION_VISIBLE_KEY, false);
    if (keepCurrentLocationVisible) {
      Location location = (Location) savedInstanceState.getParcelable(CURRENT_LOCATION_KEY);
      if (location != null) {
        setCurrentLocation(location);
      }
    }
  }

  /*
   * At this point, after onCreateView, getMap will not return null and we can
   * initialize googleMap. However, onActivityCreated can be called multiple
   * times, e.g., when the user switches tabs. With
   * GoogleMapOptions.useViewLifecycleInFragment == false, googleMap lifecycle
   * is tied to the fragment lifecycle and the same googleMap object is
   * returned in getMap. Thus we only need to initialize googleMap once, when
   * it is null.
   */
  if (googleMap == null) {
    googleMap = getMap();
    googleMap.setMyLocationEnabled(true);

    /*
     * My Tracks needs to handle the onClick event when the my location button
     * is clicked. Currently, the API doesn't allow handling onClick event,
     * thus hiding the default my location button and providing our own.
     */
    googleMap.getUiSettings().setMyLocationButtonEnabled(false);
    googleMap.setIndoorEnabled(true);
    googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
      public boolean onMarkerClick(Marker marker) {
        if (isResumed()) {
          String title = marker.getTitle();
          if (title != null && title.length() > 0) {
            long id = Long.valueOf(title);
            Context context = getActivity();
            Intent intent = IntentUtils.newIntent(context, MarkerDetailActivity.class)
                .putExtra(MarkerDetailActivity.EXTRA_MARKER_ID, id);
            context.startActivity(intent);
          }
        }
        return true;
      }
    });
    googleMap.setLocationSource(new LocationSource() {

        @Override
      public void activate(OnLocationChangedListener listener) {
        onLocationChangedListener = listener;
      }

        @Override
      public void deactivate() {
        onLocationChangedListener = null;
      }
    });
    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
      public void onCameraChange(CameraPosition cameraPosition) {
        if (isResumed() && keepCurrentLocationVisible && currentLocation != null
            && !isLocationVisible(currentLocation)) {
          keepCurrentLocationVisible = false;
        }
      }
    });
  }
}