I'm working on an application that uses Google Maps API.
My map is displayed with a defined bearing and rotation/compass are disabled.
I'd like to add Camera Bounds to my map so that the user does not leave the area of interest.
However the API seems to provide only one method for that, which is
GoogleMap.setLatLngBoundsForCameraTarget(LatLngBounds)
The issue is that LatLngBounds are "boxes" aligned with latitude and longitude.
So on my map that has a bearing, the camera bounds have a diamond shape instead of a square one, which is a bit weird.
Is there a way I can set some Camera Bounds that are not Latitude/Longitude aligned ?
I thought of writing a workaround with a CameraMoveListener that would trigger a CameraUpdate, but it's a bit of work for a result that I believe would be not that great in term of user experience.
Related
I am developing an android application using Google maps with clustering of markers. The application works fine however once you zoom into a specific area and all the clusters expand to individual markers I would like to also show a small view that represents the entire map, where you are, and where all the markers are, to enable the user to navigate round the map while zoomed in. Ive Googled and searched SO, but not found anything. Is there any "off the shelf" solution? Or am I going to have to code this all myself? The type of solution I am looking for is the type of small window that many games use to show an overview of where the player is and all the points of interest are within the current level.
I haven't seen any "off the shelf" solutions, but here is what I would do here:
1) Create an additional fragment of the map. Place it in one of corners like games do
2) Here is where all the magic should happen. You need to synchronize these 2 maps. Map's Projection lets you to translate geo coordinates into XY coordinates within the view. That's being said you can get lat/lng of your top-left and bottom-right corners on the main map and translate these 2 corners into top-left and bottom-right XY coordinates of the mini-map's view. Now, when you have XY coordinates, the only thing you need to do - is to draw a rect on top of the mini map.
There is even easier and more native solution - since you have top-left and bottom-right lat/lng coordinates - you can draw a set of polylines on a minimap to get a rect. But in this case it will be hard to move it around (with your finger) in case you want to change current main map location using mini-map
Consider I have the LatLng of location A in the map. how can I get the Latlng of B and C position on the map? I want to get the bounds of the red box.
EDIT: I want to get the bound of this box and limit the camera to that bound, so user can not change the camera and go outside this bound
The code I posted in this answer: MKCoordinateRegionMakeWithDistance equivalent in Android will get bounds for you.
Usage:
LatLngBounds bounds = boundsWithCenterAndLatLngDistance(centerPointPosition, 12000, 12000); // 12000 in meters
This answers your original question about getting bounds. You can also get point B and C from bounds easily.
Now the problem is you cannot restrict user panning with the current API. You may try to use onCameraChange to detect when you are outside and force it back, but that won't give any good user experience in my opinion.
I think you are better of moving camera to this bounds only once and giving them free hand to swipe to Africa anytime they want. In the end they will use app functionality, which is in your bounds.
I'm trying to create a custom "my location" button for my app using Google Maps.
What I'm trying to do is to center the map around the location of the user, which is already done and working flawlessly, but also want to zoom in just enough to see a marker of my choice (this is actually the closest of a set of markers I have in memory, but that's not important now).
I haven't been able to find how the zoom variable works here. If I know the marker I want to show is 0.5 GPS units away from me, how can I center the map around me in a way that includes that marker on its boundaries? I'd also use a padding to make sure it perfectly fits in the map.
LatLng my_coordinates = ...;
LatLng closest_mark = ...;
map.animateCamera(CameraUpdateFactory.newLatLng(my_coordinates));
So now I want to modify that code to not only center the position to my_coordinates but also make sure zoom will make closest_mark fit in the viewport
CameraUpdateFactory.newLatLngBounds( ?? , /*padding*/);
I don't think there is a specific zoom variable in the api you can control along with the points in the map. (And LatLngBounds just takes in the upper right and lower left bounds and gets you a view accordingly).
I believe what you could do is with a little geometry. If your marker is very close to your location (you can consider it a rectangle), get the distance between the two and extrapolate that on the opposite direction with the same distance (multiple by a small factor if you want some padding) to get the other coordinates, and then you can get the upper right and lower left coordinates (simple geometry).
If your marker is quite far and the surface of the earth comes into picture, you may have to use the haversine formula (great circle distance).
Hope this helps.
I want to build an app that uses GPS data and a building map I provide to show the user where in the building on the map they are. This will be done in a specific building that i already know gets GPS and cell service.
At first I thought the easiest way to do this was to see if I could use Google maps to plot the users location and then just "overlay" my custom building map on top of the Google map so that I wouldnt have to deal with any of the gps information or the complexities of the mapping I would just have to scale my "overlay" to fit properly on top of the Google map so that the user was shown in the correct room in a building. I'm wondering if anyone can provide me any information on how to do this or if there is an easier way to accomplish my map. Any information at all is helpful!
You want...
Google Map View
...and more specifically you will probably want to read the subsection appropriately titled: "Part 2: Adding Overlay Items"
EDIT: Whoops! Nevermind! I misread your question... that is only if you want to overlay an item on the map. Sorry...
There is no possibility to use closer zoom level than that you can see on standard GMap i.e. in browser. Other problem is that google uses GeoPoint class based on cardinal microdegrees to draw overlays, and it's accuracy is to low.
You can look on jGarminImg - it's java library - unfortunately written for using with swing, but it should be relatively easy to make it work with android. On the other hand - you have to make your own map.
You can use standard overlays, or you can make your map in kml format and use this example to display it.
You may be able to achieve this with a custom view that displays your building plan and knows the precise co-ordinates of each corner of the building.
When you receive your location updates you can add a marker to your custom view by translating the real world position into a position in the image using something along the lines of:
pseudocode:
markerX = realWorldX - mapStartX;
markerY = realWorldY - mapStartY;
if( isOnMap( markerX, markerY ) )
{
drawMarker( markerX, markerY );
}
Yes you can overlay bitmap images on top of the Google MapView.
All you have to do is subclass the Overlay class, override the draw method, and draw on the canvas. You have to provide a rectangle of GeoPoints (probably the top left and the bottom right corners) to anchor the building bitmap on top of the MapView. You use mapView.getProjection() to translate the latitude and longitude into xy coordinates on the canvas.
I assume drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) will be useful here. Bear in mind that src and paint can be null. If the GeoPoints you used are accurate, the bitmap will adjust automatically to pans and zooms, although it might get pixelated if the user zooms in too much.
edit: I am not so confident that Google Maps will have your building stays at the exact same GeoPoints in different zoom levels, so you might have to adjust those values for different zoom levels
If you need only the map of the building, it should not be too difficult to plot the location on an image without using Google Maps, provided that you can determine your location as coordinates inside the building.
You need to know two coordinates: north-west and south-east corners of the building map you are using. When you get GPS location updates, the correct location on the map image can be easily calculated based on these corner coordinates.
I would do it like this,
Place a marker on the google map to indicate the position of the building
Drilling down on the building would load your building map as a custom view. Plot the user location on the custom view
I think trying to overlay your building map on a google map while possible will be more complex to code than doing it via a custom view.
Also overlaying the lowest zoom level with your building map is not going to give you enough resolution unless you have a thumping big building. Whole blocks are pretty small
One issue you have probably already considered is the device will revert to cell tower and wifi for it's location when inside the building giving you a less accurate location fix.
I am using the google maps api for an android game that incorporates the players movement in the real world. I have figured out how to make markers indicating where something would be on the map, but I would like to keep a fixed map zoom level and have off-screen markers show up as arrows on the screen.
Anyone know of a way inside of the map API to do this without having to explicitly figuring out the angle to the object and rotating an arrow icon to point in that direction.
I don't have experience of the Google API, but from a simple point of view if you know the current location and the marker location couldn't you just use some simple vector maths to calculate the heading and, therefore, the arrow heading?
i.e.
MarkerPosition - CurrentPosition = TargetVector
TargetVector.normalize()