Reputation: 5313
I am working on an Android project in which I am working on Maps functionality. In the maps, I want to add markers at random locations around me, scattered around a bit randomly, and by default the map should be zoomed in to my present location. Currently, the random location are just getting added along Y-axis more, than being scattered around equally. And zoom is not working on my location. Any ideas why? Thank you..
Code :
SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
// Getting GoogleMap object from the fragment
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(12), 5000, null);
// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
locationCount = sharedPreferences.getInt("locationCount", 0);
// Getting stored zoom level if exists else return 0
//zoom = sharedPreferences.getString("zoom", "12");
gps = new GPSTracker(MapsActivity.this);
double radius = 10;
if (gps.canGetLocation()) {
longitude = gps.getLongitude();
latitude = gps.getLatitude();
Double[] gpsArray = new Double[]{longitude,latitude};
List<LatLng> randomPoints = new ArrayList<>();
List<Float> randomDistances = new ArrayList<>();
Location myLocation = new Location("");
myLocation.setLatitude(latitude);
myLocation.setLongitude(longitude);
//This is to generate 10 random points
for(int i = 0; i<10; i++) {
double x0 = latitude;
double y0 = longitude;
Random random = new Random();
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / Math.cos(y0);
double foundLatitude = new_x + x0;
double foundLongitude = y + y0;
LatLng randomLatLng = new LatLng(foundLatitude, foundLongitude);
randomPoints.add(randomLatLng);
Location l1 = new Location("");
l1.setLatitude(randomLatLng.latitude);
l1.setLongitude(randomLatLng.longitude);
randomDistances.add(l1.distanceTo(myLocation));
Marker mrk = googleMap.addMarker(new MarkerOptions().position(randomLatLng).title("Title").snippet("Snippet"));
}
//Get nearest point to the centre
// int indexOfNearestPointToCentre = randomDistances.indexOf(Collections.min(randomDistances));
// return randomPoints.get(indexOfNearestPointToCentre);
} else {
gps.showSettingsAlert();
}
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
int restoId = Integer.valueOf(marker.getSnippet());
return true;
}
});
mapsact.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
Output :
Update
Code :
Location myLocation = new Location("");
myLocation.setLatitude(latitude);
myLocation.setLongitude(longitude);
double x0 = latitude;
double y0 = longitude;
double x1 = x0;
double y1 = y0;
//This is to generate 10 random points
for (int i = 0; i < 10; i++) {
x0 = x0 + isOneOrZero();
y0 = y0 + isOneOrZero();
LatLng randomLatLng = new LatLng(x0, y0);
Marker mrk = googleMap.addMarker(new MarkerOptions().position(randomLatLng).title("Title").snippet("Snippet"));
x1 = x1 - isOneOrZero();
y1 = y1 - isOneOrZero();
LatLng randomLatLng1 = new LatLng(x1, y1);
Marker mrk1 = googleMap.addMarker(new MarkerOptions().position(randomLatLng).title("Title").snippet("Snippet"));
}
public int isOneOrZero() {
Random random = new Random();
if (random.nextBoolean()) {
return 1;
} else {
return 0;
}
}
Upvotes: 0
Views: 85
Reputation: 7085
The correct implementation is:
public static void getLocation(double x0, double y0, int radius) {
Random random = new Random();
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / Math.cos(Math.toRadians(y0));
double foundLongitude = new_x + x0;
double foundLatitude = y + y0;
System.out.println("Longitude: " + foundLongitude + " Latitude: " + foundLatitude );
}
More info visit here
Upvotes: 0
Reputation: 73
use googleMap.moveCamera("your_latlng")
for focusing on your current location
you can refer this project- https://github.com/Avi1411/GoogleMapsAPI/blob/master/MapsActivity.java
for RANDOM locations(in scattered way)
let randLatLng()
be a function which will return a number between 0 and 1
double x1=x0;
double y1=y0;
for(int i=0;i<5;i++)
{
x0=x0+randLatLng();
y0=y0+randLatLng();
//plot (x0,y0) to map
x1=x1-randLatLng();
y1=y1-randLatLng();
//plot (x1,y1) to map
}
this will give you 10 random and closed(nearby) coordinates if you need more closer coordinates then just decrease the range from 0 to 1 to 0 to 0.5
Upvotes: 1
Reputation: 1355
try this:
//Calculate the markers to get their position
LatLngBounds.Builder b = new LatLngBounds.Builder();
for (Marker m : markers) {
b.include(m.getPosition());
}
LatLngBounds bounds = b.build();
//Change the padding as per needed
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 10,10,10);
mMap.animateCamera(cu);
Upvotes: 1