Reputation: 1008
I am developing one application in that I need to pass multiple co-ordinates [Lat and Long] and display all of them on the same MAP, I am getting my current location on my map. But how do I get 5 locations in map?
my code:
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap _googleMap;
private static final LatLng GOLDEN_GATE_BRIDGE =
new LatLng(37.828891,-122.485884);
LatLng myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_googleMap = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
if(_googleMap==null){
Toast.makeText(getApplicationContext(), "Google Map Not Available", Toast.LENGTH_LONG).show();
}
LocationManager locationManger =
(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria=new Criteria();
String provider = locationManger.getBestProvider(criteria, true);
Location location = locationManger.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
double langitude = location.getLongitude();
LatLng latlang = new LatLng(latitude, langitude);
LatLngBounds curScreen =
_googleMap.getProjection().getVisibleRegion().latLngBounds;
curScreen.contains(latlang);
myPosition = new LatLng(latitude, langitude);
Circle circle = _googleMap.addCircle(new CircleOptions()
.center(new LatLng(latitude, langitude))
.radius(10000)
.strokeColor(Color.RED));
_googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
_googleMap.addMarker(new
MarkerOptions().position(myPosition).title("start"));
}
}
Upvotes: 0
Views: 553
Reputation: 47807
It's very simple try to create a Random Location
as per your Current Location
Like:
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
And add into your Map
like:
for (int i = 0; i < 10; i++) {
// random latitude and logitude
double[] randomLocation = createRandLocation(yourcurrentpositionLatitude, yourcurrentpositionlongitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1])).title(
"Hello Maps " + i);
System.out.println("latitude: " + randomLocation[0] + ", "
+ randomLocation[1]);
Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]);
Toast.makeText(
youractivity.this,
" Random Location " + randomLocation[0] + ","
+ randomLocation[0], Toast.LENGTH_LONG).show();
if (i == 0)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if (i == 1)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (i == 2)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
if (i == 3)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
if (i == 4)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
if (i == 5)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
if (i == 6)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
if (i == 7)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
if (i == 8)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
if (i == 9)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
// Add Marker belong here
mMap.addMarker(marker);
}
Output: image also contains one Cirlce
is created as Center of your Current Location
Upvotes: 3