Osmdroid maximum zoom level displaying two geo locations - android

I am using osmdroid library to render maps. I have two geo points that vary intermittently, and want the org.osmdroid.views.MapView to resize and translate making sure both points are always visible. I can easily re-centre the map on the point mid-way between the two, but how do I set the zoom level to ensure my two points are visible?
UPDATE
//Getting pickup and destination latitude and longitude
GeoPoint pickupLocation = null;
if (tripRequest.getPickupLat() != null && tripRequest.getPickupLong() != null) {
pickupLocation = new GeoPoint(tripRequest.getPickupLat(), tripRequest.getPickupLong());
}
GeoPoint destinationLocation = null;
if (tripRequest.getDestinationLat() != null && tripRequest.getDestinationLong() != null) {
destinationLocation = new GeoPoint(tripRequest.getDestinationLat(), tripRequest.getDestinationLong());
}
if (destinationLocation != null && pickupLocation != null) {
//Adding destination marker to map
Marker destinationLocationMarker = new Marker(map);
destinationLocationMarker.setPosition(destinationLocation);
destinationLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
destinationLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_green_small));
map.getOverlays().add(destinationLocationMarker);
//Adding pickup marker to map
Marker pickupLocationMarker = new Marker(map);
pickupLocationMarker.setPosition(pickupLocation);
pickupLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
pickupLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_red_small));
map.getOverlays().add(pickupLocationMarker);
BoundingBox boundingBox = BoundingBox.fromGeoPoints(Arrays.asList(destinationLocation, pickupLocation));
mapController = map.getController();
mapController.setCenter(boundingBox.getCenter());
mapController.setZoom(10);// I need to set this zoom properly
touchListener = new TouchListener();
map.setOnTouchListener(touchListener);

Use zoomToBoundingBox. It should work and if it doesn't problem will be elsewhere.
Try:
map.zoomToBoundingBox(boundingBox, false);
map.invalidate();
It this still don't work, check this response:
zoom mapView to a certain bounding box on osmdroid
If you really want to calculate zoom yourself you can always find an inspiration in source code of zoomToBoundingBox method and modify the calculation to fit your needs.

I believe your are looking for zoom to bounds which is in mapview or map controller. Use the max/min lat lon from your points. You'll want to test at the date line and equator

Related

Camera map never goes to offset position

I working with maps and in my app I have a SupportMapFragment filling whole screen. When a marker is clicked a popup shows. This popup when shown get 75% of the screen.
Because of that I need to center the marker in the remaining 25%. To do that I'm using setOnClusterItemClickListener from CluserManager. But no matter what I do the marker always goes to the center of the SupportMapFragment.
I tried most solutions from this answer:
How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)
No success. Below I put some code. What I would like to know is why marker continues to go to center of the screen.
clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem myItem) {
LatLng location = myItem.getPosition();
/*float zoom_lvl = googleMap.getCameraPosition().zoom;
double dpPerdegree = 256.0*Math.pow(2, zoom_lvl)/170.0;
double screen_height = (double) mapFragment.getView().getHeight();
double screen_height_30p = 50.0*screen_height/100.0;
double degree_30p = screen_height_30p/dpPerdegree;
LatLng centerlatlng = new LatLng( location.latitude, location.longitude + degree_30p);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(centerlatlng), 1000, null);*/
Projection projection = googleMap.getProjection();
LatLng markerPosition = myItem.getPosition();
Point markerPoint = projection.toScreenLocation(markerPosition);
Point targetPoint = new Point(markerPoint.x, markerPoint.y - mapFragment.getView().getHeight() / 4);
LatLng targetPosition = projection.fromScreenLocation(targetPoint);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(targetPosition), 1000, null);
}
});
Solved. I just need to return true. Returning false tells to ClusterManager always centers the mark on screen.

Why is projection not working using my MapView's spatial reference? (ArcGIS SDK for Android)

I have a MapView called mMapView. I want to add a point graphic to the map. Here is what I did:
Point p1 = new Point(66.9969, 6.65428); //I took this coordinate from my GPS device for demonstration
Point p2 = (Point) GeometryEngine.project(
p1,
SpatialReference.create(4326),
mMapView.getSpatialReference());
Graphic graphic = new Graphic(
p2,
new SimpleMarkerSymbol(Color.RED, 10, SimpleMarkerSymbol.STYLE.DIAMOND));
mGraphicsLayer.addGraphic(graphic);//mGraphicsLayer is my GraphicsLayer on my MapView
When I ran my program the marker was not there on my MapView. But when I replaced mMapView.getSpatialReference() with SpatialReference.create(32637) the graphic appeared in exactly the right position I wanted. Why this is happening? I checked my MapView spatial reference id and latestid and and it is 102100 and 3857 respectively.
My guess is that the map doesn't yet have its spatial reference when you call project. It's hard to say because you didn't say where in the app your code snippet is. But if you put it in your Activity's onCreate method, the map's spatial reference is probably null still. Try an OnStatusChangedListener, like this:
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
public void onStatusChanged(Object source, STATUS status) {
if (STATUS.INITIALIZED.equals(status)) {
mGraphicsLayer = new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
Point p1 = new Point(66.9969, 6.65428);
Point p2 = (Point) GeometryEngine.project(
p1,
SpatialReference.create(4326),
mMapView.getSpatialReference());
Graphic graphic = new Graphic(
p2,
new SimpleMarkerSymbol(Color.RED, 10, SimpleMarkerSymbol.STYLE.DIAMOND));
mGraphicsLayer.addGraphic(graphic);
}
}
});
Another possibility is that your coordinates are not in latitude and longitude. As written, with lon = 66.9969 and lat = 6.65428, it should be in the ocean near India. If you meant lon = 6.65428 and lat = 66.9969, it would be in the ocean near Norway. If you didn't mean either of those locations, then your coordinates are either incorrect or not in latitude and longitude.

Animating Camera with bounds returns nullPointerException

I am develepoing an app that needs to view various map points at the same time. I am trying to animate camera with bounds and Builder, but it's returning null every time, and I checked the coordinates and the .build() and all of them have data. this is the code.
LatLngBounds.Builder builderMap = new LatLngBounds.Builder();
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
if(location!=null)
{
Log.i("MapACtivity","builder includes myLocation");
builderMap.include(new LatLng(location.getLatitude(),location.getLongitude()));
coordinate_bounds= coordinate_bounds +1;
}
for(CircleContact myContact : newCircle.getMembers())
{
if(myContact.getLatitude()!=0 && myContact.getLongitude()!=0)
{
Log.i("MapACtivity","builder includes locations " + myContact.getDevice());
Log.i("MapACtivity","Lat " + myContact.getLatitude()+ " longitude" +myContact.getLongitude());
builderMap.include(new LatLng(myContact.getLatitude(),myContact.getLongitude()));
coordinate_bounds= coordinate_bounds +1;
}
}
if(coordinate_bounds>1)
{
Log.i("CircleFragment","builderMap.build()" + builderMap.build().getCenter());
Log.i("CircleFragment","builderMap.build()" + builderMap.build().northeast);
Log.i("CircleFragment","builderMap.build()" + builderMap.build().southwest);
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));
}
The log that I get is :
builderMap.build()lat/lng: (-11.075408249999999,-75.85188305)
builderMap.build()lat/lng: (-10.078819,-74.748112)
builderMap.build()lat/lng: (-12.0719975,-76.9556541)
But it's returning null in this line:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));
and it is not the map because, before I update the camera to my location, and that doesn't crash, this is the code before trying to get all positions inside the map and it is working fine:
MapView mMapView;
mMapView = (MapView)getActivity().findViewById(R.id.map_locations);
mMap = mMapView.getMap();
mMap.addMarker(myMarker);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(Location, 15);
mMap.animateCamera(yourLocation);
This comes from the official google maps android page:
Note: Only use the simpler method newLatLngBounds(boundary, padding) to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout, the API calculates the display boundaries of the map which are needed to correctly project the bounding box. In comparison, you can use the CameraUpdate returned by the more complex method newLatLngBounds(boundary, width, height, padding) at any time, even before the map has undergone layout, because the API calculates the display boundaries from the arguments that you pass.
And this is how they are setting bounds:
private GoogleMap mMap;
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(AUSTRALIA.getCenter(), 10));

Google Maps API v2 for Android: Marker and Polyline position mismatch

I'm trying to draw some markers and a polyline in the same map this way
MarkerOptions options = new MarkerOptions();
Marker marker;
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.color(context.getResources().getColor(R.color.mm_red));
if (route != null) {
for(Entry entry : route.getEntries().getEntryList()){
LatLng pos = new LatLng(entry.getGeolocation().getLatitude(), entry.getGeolocation().getLongitude());
options.position(pos);
polyLineOptions.add(pos);
marker = mMap.addMarker(options.icon(BitmapDescriptorFactory.fromResource(R.drawable.im_map_marker_main_num)));
marker.setVisible(true);
}
Polyline polyline = mMap.addPolyline(polyLineOptions);
polyline.setVisible(true);
}
However, here you can see the result I get:
As you can see, even if I pass the same LatLng position to both MarkerOptions and PolylineOptions, there is some kind of offset.
As extra information, I get this behaviour in a Nexus 7.
Thank you
Edit: The marker icon's pin location is the default one, so the center of the bottom side of it
Edit: Not bitmap creation problem. If I use canvas.drawColor, this is what I get (different size, because now I'm with a Nexus 10)
The problem here is that by default, the pin location of the Marker is at the bottom center of the image.
You will have to fiddle with anchor(float, float) and / or make sure the sharp edge is the last pixel on the bottom center (with no transparent pixels below).

Calculate area of Map in miles on Zoom Level

I am new in android, I want to know how can I calculate the area of a Map in miles on Zoom Level, so that I can call my web service and make a marker according to the distance of my current location.
Your question slightly confusing, area and distance are two different things.
Use the Location.distanceTo function to calculate distance between two points on the map.
OverlayItem item = mOverlays.get(index);
GeoPoint mPoint = item.getPoint(); // get Point of your marker
Location locationMy = m_MyLocationOverlay.getLastFix(); // get current location
if (locationMy != null)
{
Location locationPoint = new Location(locationMy);
locationPoint.setLatitude((double)mPoint.getLatitudeE6() / 1000000.0f);
locationPoint.setLongitude((double)mPoint.getLongitudeE6() / 1000000.0f);
String str = String.format("%.1f miles", locationMy.distanceTo(locationPoint) * 0.000621371192); // meters to miles
}

Categories

Resources