OSM Mapview: Show all geopoints from geotracking in the screen - android

I followed the example here: How to set screen zoom by geopoints?
But my problem is that my route is very small in the mapview. It should end near the borders.
This is how it looks with the code below:
private void centerMap(ArrayList<GeoPoint> waypoints) {
GeoPoint[] geoPoints = new GeoPoint[waypoints.size()];
for(int i = 0; i<waypoints.size(); i++) {
geoPoints[i] = waypoints.get(i);
}
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
//for (Point point : twoPoints) {
for(GeoPoint point: geoPoints){
int lat = (int) (point.getLatitude() * 1E6);
int lon = (int) (point.getLongitude() * 1E6);
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
}
mapView.getController().zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));
mapView.getController().setCenter(new GeoPoint((maxLat + minLat) / 2, (maxLon + minLon) / 2));
}

private void centerMap(ArrayList<GeoPoint> waypoints) {
BoundingBox bb = BoundingBox.fromGeoPoints(waypoints);
mapView.zoomToBoundingBox(bb);
}
And note that lat/lon values are now double by default in osmdroid.

Related

my marker in google maps keeps on changing position.Is there anyway to solve this issue?

i am trying to place a marker to a map according to location, and my activity refreshes after every 10 sec so that I can get location from database every 10.
What I am expecting is to change marker location every 10 sec but marker keeps on changing position frequently that is 5 to 6 times every 10 sec.
And my cab marker also keeps on dissapearing I this its the map.clear(), but without it 2 to 4 markers keeps on reappearing
For placing markers
LatLngBounds.Builder builder = new LatLngBounds.Builder();
ArrayList<Marker> markers = new ArrayList<>();
mMap.clear(); // to clear markers and polylines
drivLat1 = driverLocation.getLatitude();
drivLng1 = driverLocation.getLongitude();
drivLat2 = driverChangedLocation.getLatitude();
drivLng2 = driverChangedLocation.getLongitude();
bearingBetweenLocations(drivLat1,drivLng1,drivLat2,drivLng2);
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.car_marker);
markers.add(0,mMap.addMarker(new MarkerOptions().position(new LatLng(driverLocation.getLatitude(), driverLocation.getLongitude()))
.icon(icon)
.title("Driver Location")));
markers.add(1,mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("User Location")));
rotateMarker(markers.get(0), (float) bearingBetweenLocations(drivLat1,drivLng1,drivLat2,drivLng2));
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 100;
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
private double bearingBetweenLocations(double lat11 ,double long11,double lat22 ,double long22) {
double PI = 3.14159;
double lat1 = lat11 * PI / 180;
double long1 = long11 * PI / 180;
double lat2 = lat22 * PI / 180;
double long2 = long22 * PI / 180;
double dLon = (long2 - long1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
private void rotateMarker(final Marker marker, final float toRotation) {
if (!isMarkerRotating) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 1000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
#Override
public void run() {
isMarkerRotating = true;
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
float rot = t * toRotation + (1 - t) * startRotation;
marker.setRotation(-rot > 180 ? rot / 2 : rot);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}
And this is how I set accuracy, bcoz I think that this can be a problem
if (location.hasAccuracy()) {
// Accuracy is in rage of 20 meters, stop listening we have a fix
if (location.getAccuracy() < 20) {
LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
}
}
What I want is that markers only change position when data gets updated

Google Map Tile Provider (GeoServer And WMS)

WMS Webservice GeoServer WMS
I try to get Tile Information(I, J , BBOX) on selected Latitude and Longitude with zooming level in Google Map.
I used this formula to get I, J , BBOX Formula Source
private void getXYFromLatLon(double lat, double lon, final int zoom) {
int tileSize = 256;
// double initialResolution = 2 * Math.PI * 6378137 / tileSize;
double initialResolution = 156543.03392804062;
double originShift = 20037508.342789244;
// LatLong to Meter
double mx = lon * originShift / 180.0;
double my = Math.log(Math.tan((90 + lat) * Math.PI / 360.0))
/ (Math.PI / 180.0);
my = my * originShift / 180.0;
// Meter to Pixels
double res = initialResolution / (2 * zoom);
double px = (mx + originShift) / res;
double py = (my + originShift) / res;
getBoundingBox(Double.valueOf(px).intValue(), Double.valueOf(py)
.intValue(), zoom);
// Pixel to tiles
final int tx = (int) Math.ceil(px / ((tileSize)) - 1);
final int ty = (int) Math.ceil(py / ((tileSize)) - 1);
getTileBound(tx, ty, zoom, tileSize);
Toast.makeText(getApplicationContext(), "X: " + tx + ",Y: " + ty,
Toast.LENGTH_SHORT).show();
}private void getTileBound(int tx, int ty, int zoom, int tileSize) {
double[] min = pixelToMeter(tx * tileSize, ty * tileSize, zoom);
double[] max = pixelToMeter((tx + 1) * tileSize, (ty + 1) * tileSize,
zoom);
builder.append("\nMIN-X:" + min[0]).append("\nMIN-Y:" + min[1])
.append("\nMAX-X:" + max[0]).append("\nMAX-Y:" + max[1])
.append("\nI:" + (tx)).append("\nJ:" + (ty));
((TextView) findViewById(R.id.textView1)).setText(builder.toString());
/*
* Toast.makeText(getApplicationContext(), "X: " + min.toString() +
* ",Y: " + max.toString(), Toast.LENGTH_SHORT).show();
*/
}public String getTileNumber(final double lat, final double lon,
final int zoom) {
int xtile = (int) Math.floor((lon + 180) / 360 * (1 << zoom));
int ytile = (int) Math
.floor((1 - Math.log(Math.tan(Math.toRadians(lat)) + 1
/ Math.cos(Math.toRadians(lat)))
/ Math.PI)
/ 2 * (1 << zoom));
if (xtile < 0)
xtile = 0;
if (xtile >= (1 << zoom))
xtile = ((1 << zoom) - 1);
if (ytile < 0)
ytile = 0;
if (ytile >= (1 << zoom))
ytile = ((1 << zoom) - 1);
System.out.println("xtile" + xtile);
// Toast.makeText(getApplicationContext(),
// xtile + "YY" + ytile + "Zoom" + (1 << zoom), Toast.LENGTH_LONG)
// .show();
return ("" + zoom + "/" + xtile + "/" + ytile);
}private double[] pixelToMeter(int x, int y, int zoom) {
int tileSize = 256;
double initialResolution = 2 * Math.PI * 6378137 / tileSize;
double originShift = 2 * Math.PI * 6378137 / 2;
double res = initialResolution / (2 * zoom);
double mx = x * res - originShift;
double my = y * res - originShift;
return new double[] { mx, my };
}
The problem based on zooming level i'm not able to find the exact value ..
Based on correct value i to have call the WMS webservices
Thanks in advance...
http://192.168.1.102:1005/geoserver/estater/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo&FORMAT=image%2Fpng&TRANSPARENT=true&QUERY_LAYERS=buildings&LAYERS=kwt_buildings&INFO_FORMAT=application%2Fjson&propertyName=grid_id%2Cbuild_id&I=90&J=161&WIDTH=256&HEIGHT=256&CRS=EPSG%3A3857&STYLES=&BBOX=5342031.032794397%2C3420709.8898182083%2C5343254.02524696%2C3421932.882270771
I did not look at your code, but I have mine working since years, so here it is the WMS tile provider class:
public abstract class WMSTileProvider extends UrlTileProvider {
// Web Mercator n/w corner of the map.
private static final double[] TILE_ORIGIN = { -20037508.34789244, 20037508.34789244 };
// array indexes for that data
private static final int ORIG_X = 0;
private static final int ORIG_Y = 1; // "
// Size of square world map in meters, using WebMerc projection.
private static final double MAP_SIZE = 20037508.34789244 * 2;
// array indexes for array to hold bounding boxes.
protected static final int MINX = 0;
protected static final int MAXX = 1;
protected static final int MINY = 2;
protected static final int MAXY = 3;
// cql filters
private String cqlString = "";
// Construct with tile size in pixels, normally 256, see parent class.
public WMSTileProvider(int x, int y) {
super(x, y);
}
#SuppressWarnings("deprecation")
protected String getCql() {
try {
return URLEncoder.encode(cqlString, Charset.defaultCharset().name());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return URLEncoder.encode(cqlString);
}
}
public void setCql(String c) {
cqlString = c;
}
// Return a web Mercator bounding box given tile x/y indexes and a zoom
// level.
protected double[] getBoundingBox(int x, int y, int zoom) {
double tileSize = MAP_SIZE / Math.pow(2, zoom);
double minx = TILE_ORIGIN[ORIG_X] + x * tileSize;
double maxx = TILE_ORIGIN[ORIG_X] + (x + 1) * tileSize;
double miny = TILE_ORIGIN[ORIG_Y] - (y + 1) * tileSize;
double maxy = TILE_ORIGIN[ORIG_Y] - y * tileSize;
double[] bbox = new double[4];
bbox[MINX] = minx;
bbox[MINY] = miny;
bbox[MAXX] = maxx;
bbox[MAXY] = maxy;
return bbox;
}
}
And here is something on how i use it:
public static WMSTileProvider getWMSTileProviderByName(String layerName) {
final String OSGEO_WMS = "http://yourserver/geoserver/gwc/service/wms/?"
+ "LAYERS=" + layerName
+ "&FORMAT=image/png8&"
+ "PROJECTION=EPSG:3857&"
+ "TILEORIGIN=lon=-20037508.34,lat=-20037508.34&"
+ "TILESIZE=w=256,h=256"
+ "&MAXEXTENT=-20037508.34,-20037508.34,20037508.34,20037508.34&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG:3857"
+ "&BBOX=%f,%f,%f,%f&WIDTH=256&HEIGHT=256";
return new WMSTileProvider(256, 256) {
#Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
final double[] bbox = getBoundingBox(x, y, zoom);
String s = String.format(Locale.US, OSGEO_WMS, bbox[MINX], bbox[MINY], bbox[MAXX], bbox[MAXY]);
try {
return new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
}
};
}
Can't give you more code, but I hope can help you
EDIT: Given the comment, now i see what you need.
Here it is some code (old but working) on which you have to work a bit, it was a sort of hack:
private static final double[] TILES_ORIGIN = {-20037508.34789244, 20037508.34789244};//TODO Duplicate from WMS PROVIDER, put as utils
// Size of square world map in meters, using WebMerc projection.
private static final double MAP_SIZE = 20037508.34789244 * 2;//TODO Duplicate from WMS PROVIDER, put as utils
private static final double ORIGIN_SHIFT = Math.PI * 6378137d;
/**
* Transform the y map meter in y cordinate
*
* #param latitude the latitude of map
* #return meters of y cordinate
*/
private double inMetersYCoordinate(double latitude) {
if (latitude < 0) {
return -inMetersYCoordinate(-latitude);
}
return (Math.log(Math.tan((90d + latitude) * Math.PI / 360d)) / (Math.PI / 180d)) * ORIGIN_SHIFT / 180d;
}
/**
* Transform the x map meter in x cordinate
*
* #param longitude the longitude of map
* #return meters of x cordinate
*/
private double inMetersXCoordinate(double longitude) {
return longitude * ORIGIN_SHIFT / 180.0;
}
/**
* Get the Tile from x and y cordinates
*
* #param pointX x of the map
* #param pointY y of the map
* #param zoomLevel zoom of Tile
* #return the relative TileDataInfo
*/
private TileDataInfo getTileByCoordinate(double pointX, double pointY, int zoomLevel) {
final double tileDim = MAP_SIZE / Math.pow(2d, zoomLevel);
final int tileX = (int) ((pointX - TILES_ORIGIN[0]) / tileDim);
final int tileY = (int) ((TILES_ORIGIN[1] - pointY) / tileDim);
return new TileDataInfo(tileX, tileY, zoomLevel);
}
private static class TileDataInfo {
int tileX;
int tileY;
int tileZoom;
public TileDataInfo(int tileX, int tileY, int tileZoom) {
this.tileX = tileX;
this.tileY = tileY;
this.tileZoom = tileZoom;
}
}
In order to get the code right, you have to convert latitude in meters using the "inMetersYCoordinate", the longitude using "inMetersXCoordinate" and then use "getTileByCoordinate" to calculate the tile x,y,z (i,j,zoom for you)

ConcurrentModificationException when adding entries to ArrayList

I am trying to add some MapOverlays to my MapView and I get the following Error:
java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
at com.google.android.maps.MapView.onDraw(MapView.java:530)
at android.view.View.draw(View.java:13458)
...
at dalvik.system.NativeStart.main(Native Method)
This is my doInBackground() method of my AsyncTask:
Drawable marker = myContext.getResources().getDrawable(
R.drawable.marker);
Bitmap bitmap = ((BitmapDrawable) marker).getBitmap();
Drawable marker_new = new BitmapDrawable(myContext.getResources(),
Bitmap.createScaledBitmap(
bitmap, MainActivity.MARKER_SIZE * 10,
MainActivity.MARKER_SIZE * 10, true));
mapOverlays = map.getOverlays();
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
double fitFactor = 1.1;
MainActivity.mapListAddress.clear();
MainActivity.mapListTitle.clear();
MainActivity.mapListLati.clear();
MainActivity.mapListLongi.clear();
MainActivity.parseMaps();
for (int i = 0; i < MainActivity.mapListAddress.size(); i++) {
// String pointAddress = MainActivity.mapListAddress.get(i);
String pointTitel = MainActivity.mapListTitle.get(i);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(
marker_new, map);
double latitude = MainActivity.mapListLati.get(i) * 1e6;
double longitude = MainActivity.mapListLongi.get(i) * 1e6;
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(Map.this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(MainActivity.mapListLati.get(i),
MainActivity.mapListLongi.get(i), 1);
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
String pointAddress = address + "\n" + city + "\n" + country;
GeoPoint p = new GeoPoint((int) latitude, (int) longitude);
overlayitem = new OverlayItem(p, pointTitel,
pointAddress);
itemizedOverlay.setBalloonBottomOffset(40);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
int lat = p.getLatitudeE6();
int lon = p.getLongitudeE6();
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
map.getController().zoomToSpan((int) (Math.abs(maxLat - minLat) * fitFactor),
(int) (Math.abs(maxLon - minLon) * fitFactor));
map.getController().animateTo(new GeoPoint((maxLat + minLat) / 2,
(maxLon + minLon) / 2));
}
return null;
Why does this error occur and what am I doing wrong in my code?
Try to synchronize the mapOverlays.add(itemizedOverlay);. I think this is where the issue is, multiple threads trying to add at the same time.

How to set screen zoom by geopoints?

I drew a line between 2 geopoints. How do I set screen zoom by these 2 geopoints. What I want to do is to show start and finish points by default. Is there any algorithm for that?
here what I did :
private void centerMap() {
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
for (Point point : twoPoints) {
int lat = (int) (point.getLatitude() * 1E6);
int lon = (int) (point.getLongitude() * 1E6);
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
}
mc.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));
mc.animateTo(new GeoPoint((maxLat + minLat) / 2, (maxLon + minLon) / 2));
}
(Where mc is your MapController)

How do I show all of my markers in googlemaps android?

I have an array of latitude and longitude points. I use them to make markers on the map. What can I do to show all of the markers on the map at the same time. eg make a good fit so that if the markers are within a city I zoom it in to only show the city and not the whole state, country etc...
Thanks
something like this might help
private void centerGroup(int groupId){
if(groupId>0){
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
Cursor cursor = getYourLantLongs(groupId);
if (cursor.moveToFirst()){
final int LAT_INDEX = cursor.getColumnIndex(T.Waypoints.LATITUDE);
final int LON_INDEX = cursor.getColumnIndex(T.Waypoints.LONGITUDE);
do {
final int lat = (int) (cursor.getFloat(LAT_INDEX)*1E6);
final int lon = (int) (cursor.getFloat(LON_INDEX)*1E6);
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
} while(cursor.moveToNext());
mapController.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));
mapController.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLon + minLon)/2 ));
/*
final int cLat = (int)((maxLat*1E6 + minLat*1E6)/2);
final int cLon = (int)((maxLon*1E6 + minLon*1E6)/2);
final int zLat = (int)Math.abs(maxLat - minLat);
final int zLon = (int)Math.abs(maxLon - minLon);
//mapController.zoomToSpan(zLat, zLon);
mapController.animateTo(new GeoPoint(cLat, cLon));
*/
}
}
}
in short you make a square and zoom in.
Go through your array and save min and max values of latitude and longitude.
Then use
mMapController.zoomToSpan((maxLatitude - minLatitude), (maxLongitude - minLongitude));
to get the right zoom level. And
mMapController.animateTo(new GeoPoint(
(maxLatitude + minLatitude)/2 ,
(maxLongitude + minLongitude)/2 ));
to move the map to the center of your markers.
Have fun!

Categories

Resources