Menambahkan Marker dan Info Tambahan pada Marker

Permintaan umum tentang peta biasanya adalah menampilkan info tambahan tentang suatu tempat atau lokasi bila user men-tap suatu 'marker' pada map/peta. Pedoman lengkap untuk menampilkan info tentang map/peta bisa dilihat di link berikut ini: https://developers.google.com/maps/documentation/android-api/infowindows

Kita bisa menyimpan object data apapun dengan menggunakan method 'Marker.setTag()', dan mengambil object data tersebut dengan menggunakan method 'Marker.getTag()'. Berikut adalah latihan untuk menambahkan marker dan meng-asosiasikan data dengan marker tersebut.

Kita masih menggunakan file java dari MapsActivity.java dari latihan sebelumnya dan modifikasi seperti berikut ini:
package com.example.location;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
 
public class MapsActivity extends FragmentActivity implements GoogleMap.OnMarkerClickListener, OnMapReadyCallback {

private GoogleMap mMap;

private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);

private Marker mPerth;
private Marker mSydney;
private Marker mBrisbane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
/* Obtain the SupportMapFragment and get notified when the map is ready to be used*/
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

/* Add some markers to the map, and add a data object to each marker. */
mPerth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.title("Perth"));
mPerth.setTag(0);

mSydney = mMap.addMarker(new MarkerOptions()
.position(SYDNEY)
.title("Sydney"));
mSydney.setTag(0);

mBrisbane = mMap.addMarker(new MarkerOptions()
.position(BRISBANE)
.title("Brisbane"));
mBrisbane.setTag(0);

/* Set a listener for marker click*/
mMap.setOnMarkerClickListener(this);
}

/** Called when the user clicks a marker. */
@Override
public boolean onMarkerClick(final Marker marker) {

/* Retrieve the data from the marker*/
Integer clickCount = (Integer) marker.getTag();

/* Check if a click count was set, then display the click count.*/
if (clickCount != null) {
clickCount = clickCount + 1;
marker.setTag(clickCount);
Toast.makeText(this,
marker.getTitle() +
" has been clicked " + clickCount + " times.",
Toast.LENGTH_SHORT).show();
}

/* Return false to indicate that we have not consumed the event and that we wish
 for the default behavior to occur (which is for the camera to move such that the
 marker is centered and for the marker's info window to open, if it has one).*/
return false;
}
}

Jalankan di emulator Android Studio dan hasilnya seperti berikut:
Menambahkan marker dan info tambahan

No comments: