How to Implement Google Map Tile Provider in android? - android

I am working on custom map app and i want to use google map tiles for background ? There is one class TileProvider in api but i don't know how to initialize it every time i try it goes something like this ?
TileProvider tileProvider=new TileProvider() {
#Override
public Tile getTile(int i, int i2, int i3) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
};
how to override getTile function to get tiles from google map server?
Any example will be appreciated ?I know getTile function need x,y and zoom value to return tile.

So quick answer is that you can get the Google Map tiles from the following URL:
http://mt1.google.com/vt/lyrs={t}&x={x}&y={y}&z={z}
Where {t} is the type of map (satellite, road, hybrid, etc) and the other attributes are specifying the coordinates and zoom level.
The possible values for {t} are as follows:
Default - m
Roads only - h
Roads simplified - r
Satellite - s
Satellite hybrid - y
Terrain - t
Terrain hybrid - p
That being said you do want to make sure what you're doing is NOT a violation of the terms & services. Specifically Google WILL allow you to cache their tiles provided that you are doing so ONLY in order to improve performance and that you delete their tiles at least every 30 days, and finally you don't pull down their tiles in an attempt to make your own mapping service.

You may want to read the Google Maps Terms of Service - https://developers.google.com/maps/terms - specifically section 10.1.1

Related

Android - google maps gets stuck

I'm developing a App which display a Google map and a bunch of markers on it. There's a lot of markers so I divided them in smaller groups and display only those, which are in some bounds depending on the current position of the camera.
To do that I'm using the GoogleMap.OnCameraIdleListener. First I remove the listener, do my calculations and drawing and then I restore the listener to the Fragment containing my map:
#Override
public void onCameraIdle() {
mMap.setOnCameraIdleListener(null);
clearMap();
findTheMarkersInBounds();
displayTheMarkers();
mMap.setOnCameraIdleListener(this);
}
This way I only draw the markers I need to display and the performance is way better then having 1000 markers on the map at once. I also draw about the same number of polylines but that's not the point now.
For some strange reasons, after some panning and zooming the maps doesn't respond anymore. Can't zoom it nor pan it. App displays a dialog that it is not responding and I should wait or close the app. No erros are displayed in logcat. I can't exactly tell when this happens. Sometimes after the first pan, sometimes I can move around 2-3 minutes. Same thing happens on the emulator and on the physical device.
Anyone experienced something like this? Thanks!
Or am I approaching this the wrong way? How else should I optimize the map to display about 1000 markers and polylines. (The markers have text on them, so it can't be the same Bitmap and all of the polylines can have different colors and need to be clickable, so I can't combine them into one large polyline)
EDIT. A little more info about my methods:
After all the marker positions are loaded from the internal database, I do a for-loop through all of them and based on their position and I place them to the corresponding region. Its an 2D array of lists.
My whole area is divided to 32x32 smaller rectangular areas. When I'm searching for the markers to display, I determine which region is in view and display only those markers, which are in this area.
This way I don't need to loop over all of the markers.
My methods (very simplified) look like this:
ArrayList<MarkerObject> markersToDisplay = new ArrayList<MarkerObject>();
private void findTheMarkersInBounds() {
markersToDisplay.clear();
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
int[] regionCoordinates = getRegionCoordinates(bounds); // i, j coordinates of my regions [0..31][0..31]
markersToDisplay.addAll(subdividedMarkers[regionCoordinates[0]][regionCoordinates[1]]);
}
private void drawMarkers() {
if ((markersToDisplay != null) && (markersToDisplay.size() > 0)) {
for (int i=0; i<markersToDisplay.size(); i++) {
MarkerObject mo = markersToDisplay.get(i);
LatLng position = new LatLng(mo.gpsLat, mo.gpsLon);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(createMarker(getContext(), mo.title));
GroundOverlay m = mMap.addGroundOverlay(groundOverlayOptions.image(bitmapDescriptor).position(position, 75));
m.setClickable(true);
}
}
}
It is hard to help you without source code of findTheMarkersInBounds() and displayTheMarkers(), but seems, you need different approach to increase performance, for example:
improve your findTheMarkersInBounds() logic if it possible;
runfindTheMarkersInBounds() in separate thread and show not all markers in same time, but one by one (or bunch of 10..20 at one time) during findTheMarkersInBounds() searching;
improve your displayTheMarkers() if it possible, actually may be use custom drawing on canvas (like in this answer) instead of creating thousands Marker objects.
For question updates:
Small improvements (first, because they are used for main):
pass approximately max size of markersToDisplay as constructor parameter:
ArrayList<MarkerObject> markersToDisplay = new ArrayList<MarkerObject>(1000);
Instead for (int i=0; i<markersToDisplay.size(); i++) {
use for (MarkerObject mo: markersToDisplay) {
Do not create LatLng position every time, create it once and store in MarkerObject fields.
Main improvement:
This lines are the source of issues:
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(createMarker(getContext(), mo.title));
GroundOverlay m = mMap.addGroundOverlay(groundOverlayOptions.image(bitmapDescriptor).position(position, 75));
IMHO using Ground Overlays for thousands of markers showing is bad idea. Ground Overlay is for several "user" maps showing over default Google Map (like local plan of Park or Zoo details). Use custom drawing on canvas like on link above. But if you decide to use Ground Overlays - do not recreate them every time: create it once, store references to them in MarkerObject and reuse:
// once when marker created (just example)
mo.overlayOptions = new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromBitmap(createMarker(getContext(), mo.title)))
.position(mo.position, 75))
.setClickable(true);
...
// in your drawMarkers() - just add:
...
for (MarkerObject mo: markersToDisplay) {
if (mo.overlayOptions == null) {
mo.overlayOptions = createOverlayOptionsForThisMarker();
}
mMap.addGroundOverlay(mo.overlayOptions)
}
But IMHO - get rid of thousands of Ground Overlays at all - use custom drawing on canvas.
After further investigation and communication with the google maps android tech support we came to a solution. There's a bug in the GroundOverlay.setZIndex() method.
All you have to do is to update to the newest API version. The bug is not present anymore in Google Maps SDK v3.1.
At this moment it is in Beta, but the migration is pretty straightforward.

Android path follow (Google map) Programmatically

is it possible to follow a path on google map android (Programmatically) if my android device getting away from the path it notify or show alert that your not following your path
Help will be appriciated,
Thank's :)
Yes, it's possible by several ways. For example you can use PolyUtil.isLocationOnPath(LatLng point, java.util.List<LatLng> polyline, boolean geodesic, double tolerance) from Google Maps Android API Utility Library. In this case you need to check (with isLocationOnPath()) if user location laying on segment of the polyline of your path. Something like that:
if (!PolyUtil.isLocationOnPath(userLocationPoint, pathPolyline.getPoints(), true, 50)) {
// user away of pathPolyline more than 50 meters
// show your alert here
...
}
}
where 50 - is tolerance (in meters).
NB! It's not a complete solution - just approach.
Also you can use geofence monitoring for several waypoints (with a limit of 100 per device user).

What's the url to download map tiles from google maps?

I'm developing an android app using google maps android sdk v2 and want to allow users to download maps and use them offline.
To display the tiles I use a custom TileProvider implementation as described here.
I need to know the URL to download a google maps tile (vector tile if possible) based on latitude, longitude and zoom parameters (e.g. something like this )
Before anyone comments that it's violating google maps' terms, I can tell you it's ok to download a small amount of tiles specifically for this use case (see section 10.5.d in their terms here).
This is the link
http://mt1.google.com/vt/lyrs=y&x=1325&y=3143&z=13
lyrs parameters are :
y = hybrid
s = satelite
t = train
m = map
By Overriding getTile in your TileProvider, You get the needed X and Y and Zoom. there is no need to do anything.
#Override
public Tile getTile(int x, int y, int zoom) {
// DO YOUR STUFF HERE
}

Google Maps API for Android - detecting buildings/obstacles between me and location

Hypothetical question. I'm building an augmented reality app using Google Maps API for Android. I'm wondering if there's any data that I can use to determine whether a building lies between me and a specified location. I ask this because when sufficiently zoomed-in, there is clearly 3D data on the shape of buildings included on the map. I was wondering if there was a method like:
boolean buildingInTheWay(myLocation, destinationLocation);
if (buildingInTheWay) {
//Do something
} else {
//Do something else
}
Perhaps there's also something that could be done where if the route to a location is much longer than the birds-eye path to a location, there must be an obstacle in the way (imagine two parallel streets like so:
- = street
X = buildings
A = start location
B = destination location
---------C----A-------
xxxxxxxx | xxxxxxxxx
---------D----B-------
Here, A to B would return true, as the route around the buildings is a lot longer than the direct distance. But C to D would return false, as the route following a road is almost exactly the same distance.
However, that's not very accurate - what about between buildings? I wonder if each building on Google Maps has lat/lng points for each of its corners?
Any thoughts, anyone?

Google Maps advanced features in a MapActivity (Google Maps API)

I wanted to load a custom KML file on the map. I chose the simple way:
Intent mapIntent = new Intent(Intent.ACTION_VIEW, url);
And it works well, but obviously I can't control various features like custom icons for overlay items, or the popup "Loading myKml.kml..." that shows everytime I start it, etc.
First question:
Aren't there any parameters to setup when I start a Google Maps Intent, to tweak my map? I can't find anything on the documentation.
So I was thinking about using the Google Maps API for my app. Well, I've managed to load my KML file parsing it with a SAX parser and creating a custom overlay for my map.
It works, but there is a great problem:
The placemarks aren't loaded dynamically in relation to my position. They are loaded from the start to the end, and are shown on the map 100 at time.
So it was going to be harder than I thought, because I'll have to get my position from the GPS and calculate only the nearest points and draw them on the map.
Second question:
Does exist a built-in function to show only near-to-me placemarks on the map?
Thank you, guys.
2nd question. No. Have a look at LocationManager.addProximityAlert(double latitude, double longitude, float radius, long expiration, PendingIntent intent).

Categories

Resources