I am using the Utils library to add some custom markers in the map. My code is pretty simple, just a copy from the library own example:
......
IconGenerator iconFactory = new IconGenerator(getActivity());
addIcon(iconFactory, text, location, map);
private void addIcon(IconGenerator iconFactory, String text, LatLng position, GoogleMap map) {
MarkerOptions markerOptions = new MarkerOptions().
icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(text))).
position(position).
anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
map.addMarker(markerOptions);
}
But my markers have their center shifted to the right. I send a big string to text and this became clear (the arrows point how the left and right are not the same length):
I am using all that came with the library, so this is not my 9patch. How can I solve this?
(I am running it in an Android 4.0.3 tablet)
EDIT:
Ok, I tried this same code in an AVD running kitkat on a nexus 5, and the marker appears to be just fine:
Any clues if this is just a problem with 4.0.3?
Related
I have a GoogleMap (Lite Mode) object that has been properly initialized. It works properly with a single Marker and focuses on the proper area using CameraUpdate with newLatLngZoom.
However, when it comes to adding two markers, and have the map keep both in view using LatLngBounds, everything goes bonkers. The map is so zoomed out it shows the entire continent, and the two markers are on-top of each other (along with the polyline).
Kindly assume that the data from the pickup and destination is correct
Here's my code:
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
googleMap.addMarker(new MarkerOptions()
.position(destination)
.title(job.getDestinationAddress())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(new LatLngBounds.Builder().include(pickup).include(destination).build(), 0);
googleMap.moveCamera(cameraUpdate);
String pickupLatLng = job.getPickupLatLong().latitude + "," + job.getPickupLatLong().longitude;
String destinationLatLng = job.getDestinationLatLong().latitude + "," + job.getDestinationLatLong().longitude;
plotPolyLines(pickupLatLng, destinationLatLng);
Output:
Setting a zoom with latlng bounds is not an effective solution for all use cases.
Might be a related Bug Maps Lite mode with incorrect zoom level first time app starts
Try few solutions / hacks listed here : https://issuetracker.google.com/issues/36218443
Like refreshing the map by destroying and recreating
Also check if by using a single marker with bounds builder if the map sets to the proper zoom level
One last thing make sure that zoom level limits have not been applied.
try changing the line
googleMap.moveCamera(cameraUpdate);
to
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );
as suggested in How do I set default location and Zoom level for google map api v2?
or use
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
That should move the camera closer to your marker.
I wanted to draw a marker on Google map based on some location data I collected. When I tested the code using Galaxy S5 it draws the marker but when I try to do the same thing using a Motorola it fails to draw the marker at times and draws it other times. The object the marker represents is always there even in the Motorola. The Galaxy runs Android 5.1.1 and the Motorola 5.1: the code is as follows.
private MarkerOptions getMarkerForItem(Provider pro, LatLng lctionll, float brng){
MarkerOptions mo = new MarkerOptions();
isMarkerAlive = true;
mo.icon((BitmapDescriptorFactory.fromResource(R.drawable.cuteMarker)))
.anchor(0.5f, 0.5f) //so marker rotates around the center
.position(lctionll)
.rotation(brng)
.flat(true);
return mo;
}
There is similar question in SO but there is no answer regarding the hardware differences and how to deal with these issues. Can someone help?
EDIT---> The Galaxy has Google play services 9.2.56 and the Motorola has 9.4.52
Adding .visible(true) fixed it in the Motorola phone. I will test other devices and update this answer as needed.
MarkerOptions mo = new MarkerOptions().icon((BitmapDescriptorFactory.fromResource(R.drawable.movingcab)))
.anchor(0.5f, 0.5f) //so marker rotates around the center
.position(lctionll)
.rotation(brng)
.visible(true)
.flat(true);
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.
Here in the code am displaying markers but if many markers have same latitude and logitude then the icons generated must have different colors or it must be moved a little bit so that we must be able to find that its in that location. Is it possible to do?
private void drawMarker(LatLng point, TextView icTag) {
Bitmap icBitmap = null;
icGen.setStyle(IconGenerator.STYLE_ORANGE);
icGen.setContentRotation(0);
icGen.setContentPadding(10,10,10,10);
tvImage.setTypeface(imageFont);
icGen.setContentView(tvImage);
icBitmap = icGen.makeIcon();
MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.snippet("" + snip).position(point)
.icon(BitmapDescriptorFactory
.fromBitmap(icBitmap));
marker = googleMap.addMarker(markerOptions);
}
you can setup a bit of logic in your code so that older items are either bigger or with different colors or with different anchors.
Of course you need to check the distance of points before doing that, and i would suggest to use clustering which works fine for this kind of tasks
I'm drawing a lot of markers on the map and when they located close they overlap each other. So I want to hide some markers on small zoom and show more markers when user zooming map. Like more zoom in, more markes. Here is example code of activity and creating of markers, as you can see I'm using google maps android api v2:
public class MainActivity extends FragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.getUiSettings().setMyLocationButtonEnabled(true);
createMarkers(map);
}
private void createMarkers(GoogleMap map) {
double initLat = 48.462740;
double initLng = 35.039572;
for(float i = 0; i < 2; i+=0.2) {
LatLng pos = new LatLng(initLat + i,initLng);
map.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}
for(float i = 0; i < 2; i+=0.2) {
LatLng pos = new LatLng(initLat, initLng + i);
map.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}
}
It sounds like typical task for me, but i still didn't manage to find working solution. I've read this article https://developers.google.com/maps/articles/toomanymarkers but I've no idea how to implement it on android. Does anybody has some working code which can do this?
Here is my solution to this problem: https://github.com/Bersh/MarkersCluster
Maybe it's not the best solution, but it works for me. Hope it'll be usefull.
You are basically asking how to implement clustering on Android. As far as I know, there is no solution provided by Google to do this. The document you referenced in the comments (developers.google.com/maps/articles/toomanymarkers) is referring to the google maps javascript API. Unfortunately, none of the clustering related code is yet available on Android.
You will have to come up with your own algorithm to cluster Markers. The developers.google.com/maps/articles/toomanymarkers document that you pointed to may be a good starting point to decide what algorithm will work best for you (grid based clustering, distance based clustering, etc).
Another option that's slightly easier to implement (but not perfect) might be to change the Marker image to a smaller icon when the zoom level has changed so they don't overlap as much. Check out my answer to this question: https://stackoverflow.com/a/13976080/1103584
You could use that answer to determine when you've crossed a "zoom threshold" and change the Markers to something smaller once you've zoomed out passed a certain threshold and to a bigger image when you've zoomed back in. I use this trick in a couple of my own apps to change regular icons to small dot images when zoomed out, and back to the regular icon when you zoom back in.