[英]Google Maps open activity on marker click
I was making custom marker in Google Maps and want them to open activity on click.我正在谷歌地图中制作自定义标记,并希望他们在点击时打开活动。 I used SetOnMarkerClickListener,but it doesn't seem to work.
我使用了 SetOnMarkerClickListener,但它似乎不起作用。 Where did i go wrong?
我在哪里 go 错了? And how can i make my code better and cleaner.
我怎样才能让我的代码更好更干净。 Code listed below.
下面列出的代码。
public class Location extends FragmentActivity implements OnMapReadyCallback{
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
LatLng University = new LatLng(43.242007480536806, 76.95185128808076);
LatLng AgroBio = new LatLng(43.24019774903174, 76.95182725886109);
LatLng WaterLandForest = new LatLng(43.24203640423325, 76.95286753906628);
LatLng HydraTech = new LatLng(43.24064521255793, 76.95167663826041);
LatLng IT = new LatLng(43.23943180174249, 76.95194217695317);
LatLng Vet = new LatLng(43.24140285274656, 76.93963598030956);
LatLng TechBio = new LatLng(43.24111066883304, 76.93696680532199);
LatLng AgriTech = new LatLng(43.24118832901344, 76.95236044889603);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(University, 17));
map.addMarker(new MarkerOptions().position(University).icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_map_university)));
map.addMarker(new MarkerOptions().position(AgroBio).icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_map_university)));
map.addMarker(new MarkerOptions().position(WaterLandForest).icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_map_university)));
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
if(marker.equals(University)){
Intent intent = new Intent(Location.this, o_kaznau.class);
startActivity(intent);
return true;
} if(marker.equals(AgroBio)){
Intent intent = new Intent(Location.this, agrobio.class);
startActivity(intent);
}
return false;
}
});
}
Your if condition is failing as if(marker.equals(University))
will always be false as you're comparing LatLng with a Marker.当您将 LatLng 与标记进行比较时,您的 if 条件失败,因为
if(marker.equals(University))
将始终为假。
Try:尝试:
Add this inside onMarkerClick在 onMarkerClick 里面添加这个
LatLng position = marker.getPosition();
and then in your if condition然后在你的 if 条件下
if(position.equals(University)){
....
}
And same with your second if condition as well与您的第二个 if 条件相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.