Skip to content

Instantly share code, notes, and snippets.

@tfkproject
Last active May 20, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfkproject/c3edface4a39774c5dd89cd270875a34 to your computer and use it in GitHub Desktop.
Save tfkproject/c3edface4a39774c5dd89cd270875a34 to your computer and use it in GitHub Desktop.
Klik marker untuk masuk ke activity dengan membawa parameter
/*
skema: susun marker (indexing) (1*) > klik marker > munculin infowindow/snip (2)> klik infowindow (3)> masuk ke activity (4)
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
}
//....
// (1) buat marker dan indexing masing-masing marker
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < items.size() ; i++) {
Marker marker = createMarker(
i,
items.get(i).getLat(),
items.get(i).getLot(),
items.get(i).getJudul(),
items.get(i).getDeskripsi()
);
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
// ini membuat marker marker tadi center (bound) di layar
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.25); // offset from edges of the map 25% of screen
cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
mMap.animateCamera(cu);
private Marker createMarker(int index, String lat_tujuan, String lot_tujuan, String judul, String deskripsi) {
Marker marker = null;
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.valueOf(lat_tujuan),Double.valueOf(lot_tujuan)))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(judul)
// buat snip
.snippet(deskripsi)
);
String id_marker = marker.getId();
// pake ini kalau di fragment
mSettings = getActivity().getSharedPreferences(id_marker, Context.MODE_PRIVATE);
// pake ini kalau di activity
//mSettings = getSharedPreferences(id_marker, Context.MODE_PRIVATE);
editor = mSettings.edit();
editor.putInt("id", index);
editor.apply();
// (2) munculin infowindow/snip yg sudah di buat
marker.showInfoWindow();
return marker;
}
// (3) klik infowindow/snip
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
String id_marker = marker.getId();
mSettings = getActivity().getSharedPreferences(id_marker, Context.MODE_PRIVATE);
int i = mSettings.getInt("id", 0);
// (4) masuk ke activity
Intent intent = new Intent(getActivity(), DetailActivity.class);
// bawa parameter kalo emang ada parameter.
intent.putExtra("key_id", items.get(i).getId());
// pake ini kalau di fragment
getActivity().startActivity(intent);
// pake ini kalau di activity
// startActivity(intent);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment