I have a simple app with Google Map and dynamicly loading markers. In case 2 or more markers have same lat and long only one is presented. Is there any solution for this? This is how I plot markers to map
private void plotMarkers(ArrayList<MyMarker> markers, String markerIcon)
{
if(markers.size() > 0)
{
for (MyMarker myMarker : markers)
{
// Create user marker with custom icon and other options
// Toast.makeText(getApplicationContext(), myMarker.getmLatitude().toString(), Toast.LENGTH_SHORT).show();
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory.fromResource(manageMarkerIcon(markerIcon)));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
Even if you could, how would you use these two overlapping markers? only the top one would be visible.
Instead, if two markers overlap, you could express the data they share in one marker that contains them both.
this answer might assist: Android Google Maps v2 - Add object to marker
Both markers are shown, but both on the same location and it seems to be only one, and if you click on them, only one info window will be presented.
If you really need to add two markers at the same location you may want to take a look at Google Maps Android Marker Clustering Utility. It's part of the Google Maps Android API Utility Library.
A possible workaround could be to check if there is another marker in the position you are trying to draw the marker into, and if so, move the new marker a little bit appart.
Related
I am currently coding on Android Studio for Google Maps.
I made the search function working based on the text searched on the search bar. And once the user presses "Search" the map will zoom into that selected location and place a coloured marker.
However, I would like to do it like this:
Once you select the location, you will reveal several markers based on a certain radius/ certain area.
You can add the markers to your map as invisible and compute the distance between your desired location and each marker to show those nearer than a given distance.
private List<Marker> markers = new ArrayList<>(); // List to hold your markers
Add the markers to the map and to the list:
Marker marker = mMap.addMarker(new MarkerOptions().position(yourPosition).visible(false));
markers.add(marker);
And then create a function that, given a LatLng computes the distance to each marker (I'm using SphericalUtil.computeDistanceBetween from the Google Maps Android API Utility Library) and shows the desired markers:
private void showMarkers(LatLng location, float distance) {
for(Marker marker : markers) {
if (SphericalUtil.computeDistanceBetween(marker.getPosition(), location) <= distance) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
}
I'm using Google Maps Utils library for clustering markers.
I'm adding most of markers to cluster manager. The rest I'm adding to map (I don't want them to be clustered with others). My problem is that clusters are drawn above standard markers and cover them. Is there any way to change drawing order i.e. first clusters and markers above them?
Here's part of my code:
clusterManager = new ClusterManager<>(getActivity(), map);
clusterManager.setRenderer(new MyObjectRenderer(getActivity(), map, clusterManager));
clusterManager.setOnClusterItemClickListener(this);
map.setOnMarkerClickListener(clusterManager);
for (MyObject object : list) {
clusterManager.addItem(object);
}
clusterManager.cluster();
for (MyOtherObject object : otherList) {
map.addMarker(new MarkerOptions().position(object.getLatLng()).title(object.getName()));
}
I had a similar scenario: I was adding several locations to my ClusterManager and I needed a marker with the user position on top of all the other markers.
This worked for me:
Add user marker to map with zIndex to 1000 to ensure that it will be placed on top.
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(userLocation.getLatitude(), userLocation.getLongitude())).zIndex(1000);
map.addMarker(markerOptions);
Add markers to cluster manager.
for (MyObject object : objects) {
mClusterManager.addItem(object);
}
mClusterManager.cluster();
I'm new to android development.
I need to load a custom google-map someone made in google maps (a map with marks and information).
My question is how to load this map on a android app (and not just creating a new map and manually adding all the markers and information -- because than each time client wants to change something in the map he will need to change it in 2 places).
I know i can just make a webview but than i can't access the location and focus on users location -- is there any other way?
Thanks for the help!
You can take the location of your indicated region and add the markers manually if there isn't place to mark.
static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
https://developers.google.com/maps/documentation/android-api/marker
I've been trying to work out a method for my application to indicate when there are markers on the map that are outside of the current view/screen/bound/VisibleRegion.
For example, if there are current 5 markers on the map but the user is zoomed into a part of the map where they can't see any of the markers then I would like to be able to flag up to the user that there are markers on the map that they cannot see or something along those lines (it would be nice to be able to indicate in which direction the markers are from the current position).
I thought of using
LatLngBounds currentScreen = googleMap.getProjection()
.getVisibleRegion().latLngBounds;
but this would only be able to tell me which markers are in the current visibleRegion.
any help would be appriciated, thanks.
First, you should save all your markers somewhere. For example, in list
List<Marker> markers = new ArrayList<>();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(55.123, 36.456)));
markers.add(marker);
After this you can check, are there markers outside your screen
LatLngBounds currentScreen = mMap.getProjection().getVisibleRegion().latLngBounds;
for(Marker marker : markers) {
if(currentScreen.contains(marker.getPosition())) {
// marker inside visible region
} else {
// marker outside visible region
}
}
I am using Google map V2 to put several markers on map. i am using default marker with different color, my problem is that when i perform zoom in or out operation marker changes its position.
I have searched a lot but didn't get any solution.
to add marker i am using following code
myMap.addMarker(new MarkerOptions().position(new LatLng(serverResponse.get(i).getLat(), serverResponse.get(i).getLon())).title(data).snippet(data2));
You have forgotten to call to the method
.anchor(float a, float b)
when you are adding your maker.
Please try this:
myMap.addMarker(new MarkerOptions().position(new LatLng(serverResponse.get(i).getLat(), serverResponse.get(i).getLon())).title(data).anchor(0.5f, 0.5f).snippet(data2));