I use OSMdroid to display an offline map. The MapView is placed inside a Fragment. I would like to zoom the map to cover two points (top_left, bottom_right). Currently when I call zoomToSpan() in onViewCreated() the app freezes. I have tried using BoundingBoxE6 and latitude, longitude span but nothing changes.
BoundingBoxE6 bb = new BoundingBoxE6(
LocationConstants.TOP_LEFT_LATITUDE,
LocationConstants.TOP_LEFT_LONGITUDE,
LocationConstants.BOTTOM_RIGHT_LATITUDE,
LocationConstants.BOTTOM_RIGHT_LONGITUDE);
or
int latitudeSpan = (int) Math.abs(LocationConstants.TOP_LEFT_LATITUDE*1E6 - LocationConstants.BOTTOM_RIGHT_LATITUDE*1E6);
int longitudeSpan = (int) Math.abs(LocationConstants.TOP_LEFT_LONGITUDE*1E6 - LocationConstants.BOTTOM_RIGHT_LONGITUDE*1E6);
give the same results.
Related
I have a boundary on map and i saved all those tapped co-ordinates of that boundary. I want to show that complete boundary when i opens the map. How can i achieve it? I tried it by changing the (zoom level) camera position but not getting.
Actually my intention is how to pass more than 5 co-ordinates to map so that it shows all those passed co-ordinates on screen (fix the passed co-ordinates in mobile screen and show it when open it).
I tried this but not working perfectly, i want to change the zoom level dynamically based on passed co-ordinates.
private void showBoundary() {
final LatLng hmt = new LatLng(17.42214430,78.5483395);
final LatLng tharnaka = new LatLng(17.426542300,78.53627059999997);
final LatLng habsiguda = new LatLng(17.40681270000,78.547629599999940);
final LatLng uppal = new LatLng(17.401809200,78.56018919999997);
LatLngBounds bounds = new LatLngBounds.Builder()
.include(hmt)
.include(tharnaka)
.include(habsiguda)
.include(uppal)
.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,50));
}
Actually my intention is how to pass more than 5 co-ordinates to map so that it shows all those passed co-ordinates on screen (fix the passed co-ordinates in mobile screen and show it when open it).
I am trying to create a maps app for a certain city that have some stored latitude and longitude for certain landmarks in the city. In the map,
you can only zoom in and zoom out within the boundaries of the city
While the app is open, when you reach a certain range of lat and long coordinates within a certain radius around the landmark, it will trigger and activity that will display details about the landmark and also a voice recording about the landmark
also, the map must also have the "directions" functionality in it, where it can show several possible ways for you to get to a certain location (like landmark) from your present location and also display the distance between two points
I've already tried a GPS program from androidhive that detects your lat and long coordinates. I'm also trying to understand how to acquire and use the google maps api. I would like to know the possible approaches in doing it since I'm still new to android.
Thanks in Advance!
you can zoom with specific mile or kmeter by this code:this is for two mile:
Display display = getWindowManager().getDefaultDisplay();
// double equatorLength = 3218; // in meters
double equatorLength = 40075004; // in meters
double widthInPixels = display.getWidth();
double metersPerPixel = equatorLength / 256;
int zoomLevel = 1;
// 2 mile=3218 mtr
while ((metersPerPixel * widthInPixels) > 3218) {
metersPerPixel /= 2;
++zoomLevel;
}
return zoomLevel;
hope this is helpfull for "you can only zoom in and zoom out within the boundaries of the city"
Instead of having a polygon with a solid line surrounding it I want to create one with a dotted line, is this possible?
I know you could do this when you override the onDraw method of the overlay in v1 but the Overlay class does not exist anymore so how else can I achieve this?
It's currently not possible, but you may upvote this enhancement here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=4633
UPDATE
Recently, Google implemented this feature for polylines and polygons in Google Maps Android API v2 and marked issue 4633 as Fixed.
See information about stroke patterns in the Shapes Guide. See an example in the Polylines and Polygons tutorial.
You can also read the corresponding blog post here:
https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html
First of all, take a look on the API
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polyline
it is not yet possible with v2, but on v3 javascript API, it already is, look here:
https://developers.google.com/maps/documentation/javascript/overlays#PolylineSymbols
But it seems that it's possible to use this v3 javascript API in an android app, look here:
https://developers.google.com/maps/articles/android_v3
Maybe, this will help you
Find a LatLng at a distance of radius units from center LatLng on Map
now convert both these LatLngs to screenCoordinates
Use the formula used to construct a cirle x = Rsin(theta) , y = Rcos(theta)
you divide the circle into N segments and then draw polylines(drawn on map) on the circumference of the circle converting the screen coordinates to LatLngs
more the number of N more it looks like a circle , I have used N = 120 according the zoom level ,I am using 13.
private void addDottedCircle(double radius) {//radius is in kms
clearDottedCircle();
LatLng center,start,end;
Point screenPosCenter,screenPosStart,screenPosEnd;
Projection p = mMap.getProjection();
center = searchCenterMarker.getPosition();
start = new LatLng(center.latitude + radius/110.54,center.longitude);
// radius/110.54 gives the latitudinal delta we should increase so that we have a latitude at radius distance
// 1 degree latitude is approximately 110.54 kms , so the above equation gives you a rough estimate of latitude at a distance of radius distance
screenPosCenter = p.toScreenLocation(center);
screenPosStart = p.toScreenLocation(start);
double R = screenPosCenter.y - screenPosStart.y;
int N = 120;//N is the number of parts we are dividing the circle
double T = 2*Math.PI/N;
double theta = T;
screenPosEnd = new Point();
screenPosEnd.x = (int)(screenPosCenter.x-R*Math.sin(theta));
screenPosEnd.y = (int) (screenPosCenter.y-R*Math.cos(theta));
end = p.fromScreenLocation(screenPosEnd);
for(int i =0;i<N;i++){
theta+=T;
if(i%2 == 0){
//dottedCircle is a hashmap to keep reference to all the polylines added to map
dottedCircle.add(mMap.addPolyline(new PolylineOptions().add(start,end).width(5).color(Color.BLACK)));
screenPosStart.x = (int) (screenPosCenter.x-R*Math.sin(theta));
screenPosStart.y = (int) (screenPosCenter.y-R*Math.cos(theta));
start = p.fromScreenLocation(screenPosStart);
}
else{
screenPosEnd.x = (int)(screenPosCenter.x-R*Math.sin(theta));
screenPosEnd.y = (int) (screenPosCenter.y-R*Math.cos(theta));
end = p.fromScreenLocation(screenPosEnd);
}
}
}
If you are still looking for an answer have a look at this :
How to draw dashed polyline with android google map sdk v2?
I'm working on a mapping app that plots pins on a MapView based on a user's query. I'm trying to scale the map to fit all the results pins, but I've run into a seemingly strange situation.
I have two variables set up:
latSpan is the difference between the maximum latitude and minimum latitude of any of the results points
lonSpan is the difference between the maximum longitude and minimum longitude of any of the results points
This method
while ((mapView.getLatitudeSpan()) < latSpan) || (mapView.getLongitudeSpan() < lonSpan)){
mapController.zoomOut();
}//end of while loop
is supposed to zoom out to make sure all the pins fit on the viewable map screen.
But I'm experiencing something rather strange. The results of mapView.getLatitudeSpan() and mapView.getLongitudeSpan() are routinely greater than my latSpan and lonSpan values, so the MapController doesn't zoom out enough.
My map is zoomed in pretty far--level 15 or higher.
As an example, one set of search results gave the following values:
latSpan = 17928
lonSpan = 11636
mapView.getLatitudeSpan() = 21933
mapView.getLongitudeSpan() = 20598
Based on these numbers, you wouldn't think that the MapController would need to zoom out. Yet there are pins plotted both above the top and below the bottom of the screen. I changed my WHILE loop to read
while ((mapView.getLatitudeSpan() - 6000) < latSpan...
and that helps, but the right query will still cause issues.
But the real question is, why is this happening?
I'm not sure why you're code isn't working from the snippet provided. Its possible that you are not converting your latSpan and lonSpan to microDegrees (as shown below) and this would cause some issues.
Also if you're trying to make sure your mapView is showing all of the results, there's not much point trying to determine if it needs to zoom before zooming, just zoom it every time. If it turns out that it doesn't need to zoom then nothing will appear to happen and if it does then it does.
You can set a map up to encompass all of your points and move to the centroid of the points as follows:
GeoPoint max = new GeoPoint(maxLatitude, maxLongitude);
GeoPoint min = new GeoPoint(minLatitude, minLongitude);
int maxLatMicro = max.getLatitudeE6();
int maxLonMicro = max.getLongitudeE6();
int minLatMicro = min.getLatitudeE6();
int minLonMicro = min.getLongitudeE6();
GeoPoint center = new GeoPoint((maxLatMicro+minLatMicro)/2,(maxLonMicro + minLonMicro)/2);
controller.zoomToSpan(maxLatMicro - minLatMicro, maxLonMicro - minLonMicro);
controller.animateTo(center);
I want to zoom in or out of the map depending on where markers on my map are placed. The markers are dynamic so I can not chose a static zoom level. I understand that there is 'zoomToRegion' method in iOS to acheive this. Is there something similar in Android?
Yes.
You have to use the method zoomToSpan of MapController.
// zoom into map to box of results
int centerLat = (int)(((maxLat-minLat)/2+ minLat)*1E6);
int centerLon = (int)(((maxLon-minLon)/2 + minLon)*1E6);
mapView.getController().animateTo(new GeoPoint( centerLat, centerLon));
mapView.getController().zoomToSpan((int)((maxLat-minLat)*1E6), (int)((maxLon-minLon)*1E6));