Custom Overlays in Google Maps API v2 - android

I need to create some custom polygon overlays on my map and by custom I mean I need to add more information to the overlay so that when it gets clicked I can show a dialog or something with information about the overlay.
I have done a little reading and it looks like the Overlay and ItemizedOverlay classes were removed from the API v2 in the form of Polygons,Polylines etc...
In order to create a custom overlay it looks like I have to create a View on top of the map and just draw on the view but some of my polygons might not be visible for the current map projection
Is there anyway to assign an id to the polygon class or something so that I know what was clicked so I can get the information?

First of all GMaps Android API v2 doesn't provide a callback for when a polygon is clicked. You would have to use onMapClick and use point inside polygon algorithm iterating over all polygons.
If you keep Map you can iterate over keys and if you have a match, get the value.
Alternatively you may want to try Android Maps Extensions, which has a GoogleMap.getPolygons() for you to iterate over and Polygon.setData(Object) + Polygon.getData() to assign any additional data and retrieve it when you find a match using point inside polygon algorithm.

Related

How to get maps rendered inside of a CardView

I'm wondering how to get the map to appear inside the CardView, like in the images below, highlighted in pink.
On the left is Strava, it shows the map with the route highilghted. On the right is Google Calendar, it has a map of the location of the calender event.
I would really appriciate some pointer on where to look in the apis to achieve this.
Regards,Luke
You need to use a MapView to achieve this.
This is part of Androids Lite Mode.
Create your card, and add the MapView inside the CardView.
Then in your onCreate(), you create the actual map and set the MapView to display that map.

MapView within ExpandableListView

In my ExpandableListView I need to add Google Map on each item.
I tried with putting Map on each item but its giving me inflate exception. And its is also stated that an MapActivity can contain only one MapView.
Is there any other idea to achieve this.
For refrence you can see here.
Your comments regarding MapActivity indicate that you are using Maps V1, which is deprecated. Maps V2 does not require a MapActivity and can have multiple maps.
That being said, your proposed implementation is unlikely to work well, as putting scrollable things in scrollable things can be problematic. It also is rather unrealisitc IMHO -- whoever came up with that design has never used an actual smartphone, as there's no point in having an interactive map that small (again, IMHO).
Consider using static maps (there's a Google API for that somewhere) for inside the ExpandableListView, where a tap on the map brings up the full map, either in your app or in the standard Maps app.
I recommend to not use static map (as for example it's not possible to zoom to particular bounds with newLatLngBounds or add a circle in particular lat/lon).
I recommend to use map fragment in lite mode:
In addition, there is also a new ‘lite mode’ map option, ideal for situations where you want to provide a number of smaller maps, or a map that is so small that meaningful interaction is impractical, such as a thumbnail in a list. A lite mode map is a bitmap image of a map at a specified location and zoom level.
Map fragment can be nested in the list.

How to add a button to a API v2 Google Maps marker

I am working on an android Google Maps app, and am having trouble with map markers.
This question might have a trivial solution but after searching, I have only found links on how to add a marker with a button. What I want is different: I want a marker whose respective dialog box has a button.
Has anyone done anything similar to this?
While you can create an InfoWindowAdapter, and attach it to your GoogleMap via setInfoWindowAdapter(), to tailor the contents of an info window to contain a Button, the Button will not be clickable. The View you return from getInfoContents() in InfoWindowAdapter is not displayed. Rather, it is converted into a Bitmap, and that image is what is displayed.
However, you can find out when the user taps on the info window via an OnInfoWindowClickListener registered via setOnInfoWindowClickListener() on your GoogleMap.
The remarks from #CommonsWare about the InfoContent getting converted to a Bitmap are true. Therefor you cannot use the InfoWindows. Try to use the GoogleMap.OnMarkerClickListener event instead to display your own view on top of the MapView. The GoogleMap.OnMarkerClickListener will help you to use clicked marker as a parameter.
If you want to display the view on the correct position, you have to calculate the screen-position. Use the Projection's toScreenLocation(LatLng location) method returning Point to convert the LatLng to the screen position w.r.t GoogleMap.
See Projection for more information.

Android: Move point on the google map when clicking somewhere on the map

I want to give my users oppertinty to choose destination. from the beginning it will already have a point on the map, and after that they can choose/change to another location if they want..
how is it possible to change the point on the map, by clicking somewhere else on the map?
I assume you're talking about a MapView within your application, and not in the Google Maps app. There's a method on a MapView called onTouchEvent(). This callback will be called when a user clicks (or touches) on the map, and you can read the location of the touch from the MotionEvent object that's passed in. From there you can decide what you want to do.
Another way is to extend the Overlay class, and add it to your MapView's Overlay list. Overlay has a method called onTap(), which gives you the GeoPoint where the touch took place. You can then animate to that spot using the MapController to center the map on that spot.
If all you want to do is pan the map sideways, a user can simply touch and drag the map.
For these things to work the MapView must be clickable (settable in XML or with code).
The relevant reference pages are here:
URL to Android Maps API
i think this tutorial is perfect one. You should read carefully content of source code. That's all what u need to do.

Adding markers to a Google map on Android

I've got a place I want to add on my map at a particular lat / lng. I'd like to display that on my map when the activity starts. How would I do this?
Thanks.
This Google/Android tutorial should provide exactly the information you need.
Basically you create an custom ItemizedOverlay, grab a list of overlays from the MapView (getOverlays), create some GeoPoints and your set.
The Google Maps library documentation is a great reference for map related questions.

Categories

Resources