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://ift.tt/nIICcg"
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://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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 :
Aucun commentaire:
Enregistrer un commentaire