How can I add styled round photo pinpoints similar to the pinpoints shown in the screenshot?
I'm building something like this now, but I don't like the number of "hacks" to achieve this.
Open source library or code sample will be helpful.
With v2 of the map library your possibilities are limited, since the custom drawing code basically has to be executed before you add markers to the map, even if those are not yet visible, while the previous overlay model allowed for a delayed drawing when a certain pin came into view. So in general you have to be very careful about memory, i.e. cache drawn bitmaps, and you have to look out / test for leaks in the v2 maps library when adding / clearing many custom markers.
The actual way I went down to create custom markers was the following:
create a custom drawable that encapsulates the drawing code for each of your markers in its void draw(Canvas) method
draw each marker drawable to a bitmap by creating a new bitmap using the calculated width / height of the drawable, attaching the bitmap to a Canvas and calling the draw method of the drawable with that canvas
create a bitmap descriptor via GMap's BitmapDescriptor.fromBitmap(bitmap);
add the bitmap to the MarkerOptions of the marker
Hope that helps.
Related
I'm struggling to find a solution to the following problem in Android:
there should be custom views on the map which should animate/expand/etc just like regular views. For instance I need a current user location indicator which "pulsates" on the map, or I need some kind of marker clustering with the following behavior: when I tap on the cluster all the pois from this cluster are animated out evenly spread around the cluster so you can click them individually.
I've seen solutions involving periodically drawing into bitmap canvas and then updating the marker's bitmap, but it doesn't help when you want to click on the expanded items in the bitmap, also animating a marker with updating it's bitmap would look horrible and clunky.
On iOS for example there is a class MKAnnotationView which you can override and draw however you want, why doesn't android have such a simple feature? How can this be achieved in Android? Any help is appreciated.
In the original version of the Android Google Maps API it was very easy to implement an overlay with the following code:
List<Overlay> mapOverlays = mapView.getOverlays();
AlarmOverlay alarmOverlay = new AlarmOverlay();
mapOverlays.add(alarmOverlay);
...it was then possible to override the overlays draw() method and paint vector graphics, override the overlays onTouchEvent() method and give custom controls, etc.
I am at a loss as to how to architect similar custom controls in v2, as the use-case for overlays is not mentioned in the API reference (and markers and polygons are not enough). Does anyone have a suggested way of implementing in v2?
If you need to place your own image on the surface of the earth, use GroundOverlay. The method addGroundOverlay adds such image. It takes GroundOverlayOptions that allow to specify the image size and location (in lat long terms) and also BitmapDescriptor that, among other options, can use the ordinary Bitmap as the image source. So you can create a new Bitmap, create Canvas around this bitmap and paint there.
Seems a good approach if you need to draw something really complex, something for that polygons and markers are not sufficient. Also, the old code that draws on Canvas probably can be reused.
I have not done enough testing how soon the map will be updated after we update the bitmap. In the worst case, maybe the ground overlay needs to be removed and the new ground overlay added. The bitmap itself probably still can be reused.
Putting additional component on the top may be problematic as it must support zooming, moving around and the map is now even 3D.
I Have created a guidance application which consist a map. This map is drawn dynamically using the data from the database.[rectangle coordinates] To draw the map i have used a View class and overriden the Ondraw method.
The problem is that i cant find a way to implement the zoom in functionality. I have already used Gesture Detector class to handle OnLongPress and the OnTouchEvent methods. I was thinking of the Pinch-to-Zoom-in functionality but have no idea of how to do it.
Looking forward for some great ideas. Thank you!
Classes extends
View
GesturDetector
Can you give us some more information about how you are drawing this dynamic map? For now, I'll make an assumption and run with it. If you are drawing shapes on a canvas you can point the canvas at a Bitmap to draw onto:
Bitmap myDynamicMapBitmap = new Bitmap(MAP_WIDTH, MAP_HEIGHT, Bitmap.Config.ARGB_8888);
mapCanvas.setBitmap(myDynamicMapBitmap);
// Draw your map on the canvas here
Now that you have a bitmap representation of your map, you could utilize this open source project, which is basically an adaptation of the built in Android photo gallery app that allows users to pan and zoom images:
ImageViewZoom on GitHub
I've used that project before for an app, and it works really well. You may need to tweak it some to get your desired behavior (for example disabling panning, since you didn't say you wanted that).
Then all you have really have to do is:
imageViewZoomInstance.setImageBitmap(myDynamicMapBitmap);
That view includes built-in pinch zooming. You'll probably want to merge it with your current View that you've created.
I am trying to develop a map(basically a static image) in ANDROID where I can show different items(drawn in the map itself). How can I do that. I've tried different approaches. The map is basically a static image. Please help!
More details on what you're trying to do would help to answer this question correctly. What methods have you tried already?
Are you trying to just draw static images overlaid each other in a view?
Are you trying to draw an actual geographic map with a MapView or something along those lines?
If you're trying to draw an image over another, you can do it in a number of ways.
You could use a RelativeLayout to assign absolute positions your overlay images (most likely each in the form of an ImageView). Here's a good SO answer that covers assigning absolute positions to children in an RelativeLayout: Set the absolute position of a view
You could load the base image (your map) and the overlays into Bitmap objects. Then, using a Canvas object you could draw them in specified locations. Look at the various Canvas.drawBitmap() functions to see your options on specifying the position of the Bitmap you are drawing. You could also draw shapes instead of images using the drawRect(), drawRoundRect(), drawLine(), or drawOval() methods. There are other options as well. Read the Canvas documentation.
Otherwise, if you're trying to draw a movable map of some sort, then you'll need to use a MapView or MapActivity. There are classes that you use to overlay images in specified locations on the map. This won't be a static image, but a movable map along with the overlays that stay in the specified location. You'll use the ItemizedOverlay class to act as a data structure to store your actual overlay images (wrapped in the OverlayItem class).
This is a great tutorial that goes into depth about what I'm talking about:
http://developer.android.com/guide/tutorials/views/hello-mapview.html
In my application I would like to mark different spots on a map. What I'm now wondering is if there's a way to use a simple 9-patch image and add the spot's name as text to it or would I need to draw everything myself (including the text) in the draw() method of ItemizedOverlay?
As per this Q&A:
Drawing Nine Patch onto Canvas (Android)
you should be able to load the 9-patch using Resources.getDrawable, set the drawing bounds using Drawable.setBounds, and finally draw to the canvas provided in onDraw using Drawable.draw.
If you plan on reusing the drawable, you should keep an eye out for memory leaks, per this article.