I need to store all the LatLng points of circle drawn on google map. like :
I have circle and radius(in meter). How to get that?. i tried with the code......
private ArrayList<LatLng> makeCircle(LatLng centre, double radius, float zoom)
{
ArrayList<LatLng> points = new ArrayList<LatLng>();
LatLngBounds.Builder builder = new LatLngBounds.Builder();
double EARTH_RADIUS = 6378100.0;
for (double t = 0; t <= Math.PI * 2; t += 1.0)
{
double rad = radius + zoom * EARTH_RADIUS;
double latPoint = centre.latitude + (rad / EARTH_RADIUS) * Math.sin(t);
double lonPoint = centre.longitude + (rad / EARTH_RADIUS) * Math.cos(t) / Math.cos(centre.latitude);
points.add(new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI));
Marker customMarker = map.addMarker(new MarkerOptions()
.position(new LatLng(latPoint,lonPoint)));
builder.include(new LatLng(latPoint,lonPoint));
LatLngBounds bound = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bound, width-100, height-100, 20);
map.animateCamera(cu);
}
return points;
}
but i m getting points but not on exact locations. i am getting this
How to solve this?
The 'zoom' factor is not relevant for the calculations here. Update your makeCircle() method as shown below and it will work exactly the way you want:
private ArrayList<LatLng> makeCircle(LatLng centre, double radius)
{
ArrayList<LatLng> points = new ArrayList<LatLng>();
double EARTH_RADIUS = 6378100.0;
// Convert to radians.
double lat = centre.latitude * Math.PI / 180.0;
double lon = centre.longitude * Math.PI / 180.0;
for (double t = 0; t <= Math.PI * 2; t += 0.3)
{
// y
double latPoint = lat + (radius / EARTH_RADIUS) * Math.sin(t);
// x
double lonPoint = lon + (radius / EARTH_RADIUS) * Math.cos(t) / Math.cos(lat);
// saving the location on circle as a LatLng point
LatLng point =new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI);
// here mMap is my GoogleMap object
mMap.addMarker(new MarkerOptions().position(point));
// now here note that same point(lat/lng) is used for marker as well as saved in the ArrayList
points.add(point);
}
return points;
}
I am sure it helped you :)
Related
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
I know how to find the distance between two location on android studio google map but how to calculate which distance is the nearest one compering with 3 or 4 found location?
public void onDirectionFinderSuccess(List<Route> routes) {
progressDialog.dismiss();
polylinePaths = new ArrayList<>();
originMarkers = new ArrayList<>();
destinationMarkers = new ArrayList<>();
for (Route route : routes) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 16));
((TextView) findViewById(R.id.tvDuration)).setText(route.duration.text);
((TextView) findViewById(R.id.tvDistance)).setText(route.distance.text);
originMarkers.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue))
.title(route.startAddress)
.position(route.startLocation)));
destinationMarkers.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green))
.title(route.endAddress)
.position(route.endLocation)));
PolylineOptions polylineOptions = new PolylineOptions().
geodesic(true).
color(Color.BLUE).
width(10);
for (int i = 0; i < route.points.size(); i++)
polylineOptions.add(route.points.get(i));
polylinePaths.add(mMap.addPolyline(polylineOptions));
}
}
If you want to calculate the distance,you can calculate like this way
private double caldistance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
Calculate the distance for all found locations and having smallest distance will be the nearest location.
public double distance(double lat1, double lat2, double lon1,
double lon2, double el1, double el2) {
final int R = 6371; // Radius of the earth
Double latDistance = Math.toRadians(lat2 - lat1);
Double lonDistance = Math.toRadians(lon2 - lon1);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c * 1000; // convert to meters
double height = el1 - el2;
distance = Math.pow(distance, 2) + Math.pow(height, 2);
return Math.sqrt(distance);
}
If you want to get all the nearest locations
Create two Locations like this:
Location source = new Location("");
Location destination = new Location("");
source.setLatitude(src_lat);
source.setLongitude(src_lng);
destination.setLatitude(dest_lat);
destination.setLongitude(dest_lng);
if(source.distanceTo(destination)<3000){ // 3000 meters = 3 km
// do something ...
}
I'm using this code to convert Google Map coordinates into an X,Y system on a custom overlay. This takes a latitude, longitude, and a zoom and converts it to x,y with a custom center point built in. What are the correct steps to have an x,y and convert it back to latitude and longitude.
static Double game_1_x = 1972.606;
static Double game_1_y = 3817.044;
static Double map_1_lng = 42.012002;
static Double map_1_lat = 42.850185;
static Double game_2_x = -1210.765;
static Double game_2_y = -3443.753;
static Double map_2_lng = -49.922088;
static Double map_2_lat = -83.293854;
public static String convertToXY(String lat, String lon, float zoom) {
int mapSize = 0;
if (zoom == 2.0f) {
mapSize = 1024;
} else if (zoom == 3.0f) {
mapSize = 2048;
} else if (zoom == 4.0f) {
mapSize = 4096;
} else if (zoom == 5.0f) {
mapSize = 8192;
}
Double LAT = Double.valueOf(lat);
Double LON = Double.valueOf(lon);
// get marker x value
Double markerLon = (LON + 180) * (mapSize / 360);
// convert Lat to Radians
Double markerLatRad = LAT * Math.PI / 180;
// get marker y value
Double mercN = Math.log(Math.tan((Math.PI / 4) + (markerLatRad / 2)));
Double markerLat = (mapSize / 2) - (mapSize * mercN / (2 * Math.PI));
// get map 1 x value
Double m1lng = (map_1_lng + 180) * (mapSize / 360);
// get map 2 x value
Double m2lng = (map_2_lng + 180) * (mapSize / 360);
// convert Lat to Radians
Double m1LatRad = map_1_lat * Math.PI / 180;
Double m2LatRad = map_2_lat * Math.PI / 180;
// get map 1 y value
Double mercNm1y = Math.log(Math.tan((Math.PI / 4) + (m1LatRad / 2)));
Double m1lat = (mapSize / 2) - (mapSize * mercNm1y / (2 * Math.PI));
// get map 2 y value
Double mercNm2y = Math.log(Math.tan((Math.PI / 4) + (m2LatRad / 2)));
Double m2lat = (mapSize / 2) - (mapSize * mercNm2y / (2 * Math.PI));
Double X = game_1_x + (markerLon - m1lng) * (game_1_x - game_2_x) / (m1lng - m2lng);
Double Y = game_1_y + (markerLat - m1lat) * (game_1_y - game_2_y) / (m1lat - m2lat);
return String.valueOf(X) + "," + String.valueOf(Y);
}
See if this is what you need:
mMap = mMapView.getMap();
mMap.getProjection().fromScreenLocation(Point p)
and the other way around
mMap.getProjection().toScreenLocation(LatLng l)
Look at this example:
public void start(){
//...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS, TEN_METERS, this);
}
#Override
public void onLocationChanged(Location location) {
if(location.distanceTo(_lastLocation) > TEN_KM_IN_METERS){
actionA(location);
_lastLocation = location;
} else {
actionB(location);
}
}
The implementation of Location#distanceTo(l) is pretty complicated and CPU-intensive. So i don't want to call this operation on every location update.
Question: is it any proper way to avoid unnecessary Location#distanceTo(l) calls
What i tried so far. According Wiki - Decimal degrees i do it that way:
private boolean closeTogether(Location a, Location b) {
double changeLat = Math.abs(a.getLatitude() - b.getLatitude());
final float myNaiveMax = 0.005;
if (changeLat > myNaiveMax) {
return false;
}
double changeLon = Math.abs(a.getLongitude() - b.getLongitude());
if (changeLon > myNaiveMax) {
return false;
}
return true;
}
#Override
public void onLocationChanged(Location location) {
if(!closeTogether(location, _lastLocation) && location.distanceTo(_lastLocation) > TEN_KM_IN_METERS){
actionA(location);
_lastLocation = location;
} else {
actionB(location);
}
}
I've found that the Haversine formula is very good for this. Works well for my delivery tracking application. Here's how I calculate the distance between two points. Should get you started :)
/**
* getDistanceBetweenTwoPoints
* #param p1 - First point
* #param p2 - Second point
* #return distance between the two specified points (as the crow flys)
*/
public static double getDistanceBetweenTwoPoints(PointF p1, PointF p2) {
double R = 6371000; // Earth radius
double dLat = Math.toRadians(p2.x - p1.x);
double dLon = Math.toRadians(p2.y - p1.y);
double lat1 = Math.toRadians(p1.x);
double lat2 = Math.toRadians(p2.x);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2)
* Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = R * c;
return d;
}
Edit And another
public static PointF calculateDerivedPosition(PointF point,
double range, double bearing)
{
double EarthRadius = 6371000; // m
double latA = Math.toRadians(point.x);
double lonA = Math.toRadians(point.y);
double angularDistance = range / EarthRadius;
double trueCourse = Math.toRadians(bearing);
double lat = Math.asin(Math.sin(latA) * Math.cos(angularDistance) +
Math.cos(latA) * Math.sin(angularDistance) * Math.cos(trueCourse));
double dlon = Math.atan2(Math.sin(trueCourse) * Math.sin(angularDistance) * Math.cos(latA),
Math.cos(angularDistance) - Math.sin(latA) * Math.sin(lat));
double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;
lat = Math.toDegrees(lat);
lon = Math.toDegrees(lon);
PointF newPoint = new PointF((float) lat, (float) lon);
return newPoint;
}
I've looked through the documentation of polyline and there is no option to make it dashed.
Do anybody know how to draw dashed polyline with android google map sdk v2?
Now in Polyline you can set the pattern to be Dash, Dot or Gap
simply apply the following
public static final int PATTERN_DASH_LENGTH_PX = 20;
public static final int PATTERN_GAP_LENGTH_PX = 20;
public static final PatternItem DOT = new Dot();
public static final PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);
public static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
public static final List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);
private void drawDashedLeg(GoogleMap googleMap, Route route) {
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(ContextCompat.getColor(getContext(), R.color.coolgrey));
polyOptions.addAll(route.getPoints());
polyOptions.pattern(PATTERN_POLYGON_ALPHA);
Polyline polyline = googleMap.addPolyline(polyOptions);
polylines.add(polyline);
}
It is not possible in current release. Follow this issue for updates: https://code.google.com/p/gmaps-api-issues/issues/detail?id=4633
UPDATE
Recently, Google implemented this feature for polylines 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
Alexey, I've just created a function that worked for me and I think that will help you:
public static void createDashedLine(GoogleMap map, LatLng latLngOrig, LatLng latLngDest, int color){
double difLat = latLngDest.latitude - latLngOrig.latitude;
double difLng = latLngDest.longitude - latLngOrig.longitude;
double zoom = map.getCameraPosition().zoom;
double divLat = difLat / (zoom * 2);
double divLng = difLng / (zoom * 2);
LatLng tmpLatOri = latLngOrig;
for(int i = 0; i < (zoom * 2); i++){
LatLng loopLatLng = tmpLatOri;
if(i > 0){
loopLatLng = new LatLng(tmpLatOri.latitude + (divLat * 0.25f), tmpLatOri.longitude + (divLng * 0.25f));
}
Polyline polyline = map.addPolyline(new PolylineOptions()
.add(loopLatLng)
.add(new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng))
.color(color)
.width(5f));
tmpLatOri = new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng);
}
}
I created the following function to draw dotted polyline with a list of LatLng points.
This algorithm creates lines of 0.002 kms (followed by 0.002 kms meter gaps) irrespective of zoom. This is useful when you don't want to re-plot polylines when zoom changes.
private void drawDashedPolyLine(GoogleMap mMap, ArrayList<LatLng> listOfPoints, int color) {
/* Boolean to control drawing alternate lines */
boolean added = false;
for (int i = 0; i < listOfPoints.size() - 1 ; i++) {
/* Get distance between current and next point */
double distance = getConvertedDistance(listOfPoints.get(i),listOfPoints.get(i + 1));
/* If distance is less than 0.002 kms */
if (distance < 0.002) {
if (!added) {
mMap.addPolyline(new PolylineOptions()
.add(listOfPoints.get(i))
.add(listOfPoints.get(i + 1))
.color(color));
added = true;
} else {/* Skip this piece */
added = false;
}
} else {
/* Get how many divisions to make of this line */
int countOfDivisions = (int) ((distance/0.002));
/* Get difference to add per lat/lng */
double latdiff = (listOfPoints.get(i+1).latitude - listOfPoints
.get(i).latitude) / countOfDivisions;
double lngdiff = (listOfPoints.get(i + 1).longitude - listOfPoints
.get(i).longitude) / countOfDivisions;
/* Last known indicates start point of polyline. Initialized to ith point */
LatLng lastKnowLatLng = new LatLng(listOfPoints.get(i).latitude, listOfPoints.get(i).longitude);
for (int j = 0; j < countOfDivisions; j++) {
/* Next point is point + diff */
LatLng nextLatLng = new LatLng(lastKnowLatLng.latitude + latdiff, lastKnowLatLng.longitude + lngdiff);
if (!added) {
mMap.addPolyline(new PolylineOptions()
.add(lastKnowLatLng)
.add(nextLatLng)
.color(color));
added = true;
} else {
added = false;
}
lastKnowLatLng = nextLatLng;
}
}
}
}
private double getConvertedDistance(LatLng latlng1, LatLng latlng2) {
double distance = DistanceUtil.distance(latlng1.latitude,
latlng1.longitude,
latlng2.latitude,
latlng2.longitude);
BigDecimal bd = new BigDecimal(distance);
BigDecimal res = bd.setScale(3, RoundingMode.DOWN);
return res.doubleValue();
}
Util class to calculate distance between two LatLng:
public class DistanceUtil {
public static double distance(double lat1, double lon1, double lat2,
double lon2) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
} else
return distance(lat1, lon1, lat2, lon2, 'K');
}
public static double distance(double lat1, double lon1, double lat2,
double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
}
Note: The above algorithm generates very large number of polylines which may take time to render. It is useful only when the list of points is small.