Android google maps zoom to fit mark and current position - android

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.

Related

Position infoWindow away from overlapping polyline

I'm developing an Android application which contains an activity with google maps where I plot a path between two location with polylines. I have added markers at source and destination with infowindow showing some data, but these infowindow blocks the polyline and makes user zoom the map to see the path. Is there a way to move infowindow similar to how Uber does?
Any help will be appreciated.
Thank you.
There are really two parts to this problem: (1) establishing where you want the info window and (2) how to get it there. I will be focusing mostly on (1) and try to help with references for (2).
(Note also there is a third problem which can be inferred from the animated gif: the infowindow for "Home" is adjusted because it is clipped in the final view - I've added an approach to this at the end.)
One approach to (1) is to create a bounding rectangle around the path from Point A to Point B. This is a simplification of the problem which has shortcomings to be shown but is a good initial approach. There are optimizations which I think you'll soon realize to better fit the location in extreme cases.
Use this bounding rectangle (R) to determine the minimum dimension relative to the marker anchor of interest (d1 vs d2).
Using the mininimum dimension you can then compute a screen distance from anchor to nearest side of info window (f). From this nearest point you can then determine the anchor offset of the info window.
First some pictures which serve several purposes: (a) identify key locations by label and (b) to start considering more complex problems:
The simplest case is pictured here...:
...but the the labels are bit crammed so a second looser case is pictured here:
Determining the bounding rectangle is straight forward albeit inefficient. Loop over the points of the path (from My Location to Home) and maintain the maximums and minimums of latitude and longitude (note that the -180 to 180 longitude crossover is a pervasive complexity not addressed here - just keep it in mind). This yields:
LatLng upperLeft (maxLat, minLng)
LatLng lowerRight (minLat, maxLng)
So establishing the location of the info window of A is then:
Compute the length (in meters) from A to side of rectangle by using Point A to (A lat, M lng) and Point A to (M lat, A Lng). (Use `SphericalUtil.distanceBetween`)
Determine the lesser of the two measurements, L.
Use L (distance in meters) to compute a screen pixel equivalent S.
Adjust S to offset from edge of BR (Sadj)
Use Sadj (in screen pixels) to compute the Info Window offset (in screen pixels) taking into account the Info Window anchor point and the dimension of the Info Window.
Set the info window anchor (which is relative to the marker coordinate system) and render.
Optimization: Let's say you prefer the location of 'Home' in the 2nd picture to be something like:
Then the optimization would be to consider a smaller bounding rectangle using only some subset of the closer segments of path. So this leads to the next problem of determining when to optimize using thresholds for d1/d2. This same optimization would be used when the zoom level does not include both points; just use the points on screen to compute the bounding rectangle.
For the third problem where "Home" is clipped after the zoom is to account for the dimensions of the infowindow when applying the bounding rectangle - this way the minimum dimension will always include the corresponding extent of the info window.

Using hand drawn map in android application

In my android application I want to use "handmade" maps. It's an artistic map image, based on a real map, so it's geographically accurate. The map cover a relatively small, walkable area (like a neighborhood in a city). The map is bigger than the screen size, so the user will drag the map (but always at the same scale, so no zooming). The application has to work offline, with no Internet connection, but I want to show the user's current (possibly moving) GPS position on this map. Is there any way to do this?
Yes this is possible, and in your case not so difficult.
Measure 3 reference point;
1. near or at left upper corner (NW)
2. near or at right upper corner (NE)
3. near or lef middle or right lower corner (S).
next step is to map the 3 points to pixel and screen coordinates.
Then you need to do an linear interpolation for latitude (between y/lat coordinate of Point 1 and 3)
And an separate linear interpolation between Point1 and 2 for longitude /x value of your GPS coordinate.
With this knowledge, you search further and read some poste here, etc.
You may want to look into OpenStreetMap for your mapping. It allows you to specify a custom tile source for the map. Here is a related SO question.
You will need to break your image up into pieces, this site gives a good explanation on how the tiles are formatted.

Adding markers to google map 2 depending on the zoom level

I have multiple markers set around on the map. What I want to know is how can I show only a few markers depending on the zoom level. For example: I have a zoom on the map with radius: the length between the center of the map and the bounds of the screen (lets say this is like 2km in real life not sure if this is true) so I want to show only the markers that are inside the radius. And of course if the user zooms out the radius will be recalculated again from the center point of the map to the bound of the screen. And again include markers that are inside the new radius. How can I achieve this?
I have thought about this problem and here could be an approach.
you can loop through your available markers and find distance from the center of your given circle (since we're talking about a radius) to the marker; this can be done using computeDistanceBetween(); for more info see link
If the marker lies within your radius, show, otherwise, hide using the setVisible() method. For more information, see link
Hope this gives you some idea.

Android Google map with inset overview when zoomed in

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

Android custom GPS map application

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.

Categories

Resources