只有1 onClickListener可以在谷歌地图活动中使用标记



简而言之,我们有一个带有谷歌地图的活动,并在上面绘制了一系列标记。现在我们希望能够点击所有这些标记。为此,我们使用onClickListeners。

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Alma & Cantor en Zweetkelder")) {
Intent AlmaIntent = new Intent(MapS.this, activity_acz.class);
startActivity(AlmaIntent);
return false;
}
return false;
}
}
);

googleMap.addMarker(new MarkerOptions().position(ecolab)
.title("Ecolab")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Ecolab")) {
Intent ecolabIntent = new Intent(MapS.this, activity_ecolab.class);
startActivity(ecolabIntent);
return false;
}
return false;
}
}
);

这是我们两个地点的例子。因此,所有的标记都显示在地图上,每个位置都有相同的代码结构。问题是,如果我们测试这个,只有最后一个位置的标记是可点击的。在这种情况下,ecolab标记。我们怎样才能做到所有的标记都可以点击。

还有人在这里问了一个类似的问题,答案是他有2个setContentView((。我们的情况并非如此。

编辑:

package com.example.discoverkoelak;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;



import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.maps.CameraUpdateFactory;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.AutocompletePrediction;
import com.google.android.libraries.places.api.model.AutocompleteSessionToken;
import com.google.android.libraries.places.api.net.PlacesClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapS extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationProviderClient;
private List<AutocompletePrediction> predictionList;
private Location mLastKnownLocation;
private LocationCallback locationCallback;
private final float DEFAULT_ZOOM = 18;

private View mapView;
private Marker kaka;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.kaart);
mapFragment.getMapAsync(this);
mapView = mapFragment.getView();
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MapS.this);
Places.initialize(MapS.this, ("MY_API_KEY"));


final AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
}
@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
int height = 100;
int width = 100;
BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.marker);
Bitmap b=bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
//Code die markers zet en ze klikbaar maakt.
//Hal A
googleMap.addMarker(new MarkerOptions().position(HalA)
.title("Hal A")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Hal A")) {
Intent halaIntent = new Intent(MapS.this, hala.class);
startActivity(halaIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(stuvo)
.title("Stuvo")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Stuvo")) {
Intent stuvoIntent = new Intent(MapS.this, activity_secstuvo.class);
startActivity(stuvoIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(bib)
.title("Bib")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Bib")) {
Intent bibIntent = new Intent(MapS.this, activity_onthaalbib.class);
startActivity(bibIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(acco)
.title("Acco boekenhandel")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Acco boekenhandel")) {
Intent accoIntent = new Intent(MapS.this, activity_acco.class);
startActivity(accoIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(rectoraat)
.title("Hal Rectoraat")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Hal Rectoraat")) {
Intent rectoraatIntent = new Intent(MapS.this, activity_rectoraat.class);
startActivity(rectoraatIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(spina)
.title("Spina")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Spina")) {
Intent spinaIntent = new Intent(MapS.this, activity_spina.class);
startActivity(spinaIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(stilleRuimte)
.title("Stille ruimte")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Stille ruimte")) {
Intent stilIntent = new Intent(MapS.this, activity_stil.class);
startActivity(stilIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(A301)
.title("Aula Stijn Streuvels (A301)")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Aula Stijn Streuvels (A301)")) {
Intent A301Intent = new Intent(MapS.this, activity_A301.class);
startActivity(A301Intent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(weetkelder)
.title("Weetkelder")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Weetkelder")) {
Intent weetkelderIntent = new Intent(MapS.this, activity_weetkelder.class);
startActivity(weetkelderIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(fietsena)
.title("FietsenA")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("FietsenA")) {
Intent fietsenaIntent = new Intent(MapS.this, activity_fietsena.class);
startActivity(fietsenaIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(labos)
.title("Labo's (gang 3)")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Labo's (gang 3)")) {
Intent labosIntent = new Intent(MapS.this, activity_labo.class);
startActivity(labosIntent);
return false;
}
return false;
}
}
);
//gebouw B
googleMap.addMarker(new MarkerOptions().position(B422)
.title("B422")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("B422")) {
Intent B422Intent = new Intent(MapS.this, activity_b422.class);
startActivity(B422Intent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(puc)
.title("puc")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("puc")) {
Intent pucIntent = new Intent(MapS.this, activity_puc.class);
startActivity(pucIntent);
return false;
}
return false;
}
}
);
//gebouw c
googleMap.addMarker(new MarkerOptions().position(C611)
.title("C611")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("C611")) {
Intent C611Intent = new Intent(MapS.this, activity_c611.class);
startActivity(C611Intent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(gang7)
.title("Gang 7")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Gang 7")) {
Intent gang7Intent = new Intent(MapS.this, activity_kantoren.class);
startActivity(gang7Intent);
return false;
}
return false;
}
}
);
//gebouw E
googleMap.addMarker(new MarkerOptions().position(E1001)
.title("Aula Andreas Vesalius (E1001)")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Aula Andreas Vesalius (E1001)")) {
Intent E1001Intent = new Intent(MapS.this, activity_e1001.class);
startActivity(E1001Intent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(vaardigheidscentrum)
.title("Vaardigheidscentrum")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Vaardigheidscentrum")) {
Intent vaardigheidscentrumIntent = new Intent(MapS.this, activity_vaardigheid.class);
startActivity(vaardigheidscentrumIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(IRF)
.title("IRF")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("IRF")) {
Intent IRFIntent = new Intent(MapS.this, activity_irf.class);
startActivity(IRFIntent);
return false;
}
return false;
}
}
);

googleMap.addMarker(new MarkerOptions().position(fietsene)
.title("fietsene")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("fietsene")) {
Intent fietseneIntent = new Intent(MapS.this, activity_fietsene.class);
startActivity(fietseneIntent);
return false;
}
return false;
}
}
);
//residenties
googleMap.addMarker(new MarkerOptions().position(spoelberg)
.title("Spoelberg")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Spoelberg")) {
Intent spoelbergIntent = new Intent(MapS.this, activity_spoelberg.class);
startActivity(spoelbergIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(studentendorp)
.title("Studentendorp")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Studentendorp")) {
Intent studentendorpIntent = new Intent(MapS.this, activity_studentendorp.class);
startActivity(studentendorpIntent);
return false;
}
return false;
}
}
);
googleMap.addMarker(new MarkerOptions().position(corona)
.title("Corona")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Corona")) {
Intent coronaIntent = new Intent(MapS.this, activity_corona.class);
startActivity(coronaIntent);
return false;
}
return false;
}
}
);
//andere
googleMap.addMarker(new MarkerOptions().position(IICK)
.title("IICK")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("IICK")) {
Intent IICKIntent = new Intent(MapS.this, activity_iick.class);
startActivity(IICKIntent);
return false;
}
return false;
}
}
);

googleMap.addMarker(new MarkerOptions().position(almaZweetkelder)
.title("Alma & Cantor en Zweetkelder")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Alma & Cantor en Zweetkelder")) {
Intent AlmaIntent = new Intent(MapS.this, activity_acz.class);
startActivity(AlmaIntent);
return false;
}
return false;
}
}
);

googleMap.addMarker(new MarkerOptions().position(ecolab)
.title("Ecolab")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Ecolab")) {
Intent ecolabIntent = new Intent(MapS.this, activity_ecolab.class);
startActivity(ecolabIntent);
return false;
}
return false;
}
}
);





if(mapView != null && mapView.findViewById(Integer.parseInt("1")) != null)  {
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.setMargins(0,0,40,180);
}
// kijken of gps signaal aanstaat
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient settingsClient = LocationServices.getSettingsClient(MapS.this);
Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());
task.addOnSuccessListener(MapS.this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
getDeviceLocation();
}
});
task.addOnFailureListener(MapS.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if(e instanceof  ResolvableApiException) {
ResolvableApiException resolvable = (ResolvableApiException) e;
try{
resolvable.startResolutionForResult(MapS.this, 51);
} catch (IntentSender.SendIntentException e1) {
e1.printStackTrace();
}
}
}}


);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 51) {
if (resultCode == RESULT_OK) {
getDeviceLocation();
}
}

}
@SuppressLint("MissingPermission")
private void getDeviceLocation() {
mFusedLocationProviderClient.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
mLastKnownLocation = task.getResult();
if (mLastKnownLocation != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
} else {
final LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult == null) {
return;
}
mLastKnownLocation = locationResult.getLastLocation();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
};
mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
} else {
Toast.makeText(MapS.this, "Laatste gekende locatie kan niet worden ontvangen", Toast.LENGTH_SHORT).show();
}
}
});
}
}

尝试这个

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Ecolab")) {
Intent ecolabIntent = new Intent(MapS.this, activity_ecolab.class);
startActivity(ecolabIntent);
}
else if (marker.getTitle().equals("Alma & Cantor en Zweetkelder")) {
Intent AlmaIntent = new Intent(MapS.this, activity_acz.class);
startActivity(AlmaIntent);
}
return true;
}
}
);

最新更新