Reputation: 10059
I need to point out five different locations on Google Maps. So far I got a output successfully. There was no errors in this code.
My only problem is it only points to a single location. It have to points to a different locations, for example, one have to point to Chennai and other have to point to Delhi or something else.
MainActivity.java:
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double latitude[] ={13.07891,13.11602,13.11355,13.12511,13.08367};
double longitude[] = {80.28215,80.23166,80.29613,80.29554,80.23961};
// lets place some 10 random markers
for (int i = 0; i < 5; i++) {
// random latitude and logitude
double[] randomLocation = createRandLocation(latitude[i],
longitude[i]);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1]))
.title("Hello Maps " + i);
Log.e("Random", "> " + randomLocation[0] + ", "
+ randomLocation[1]);
// changing marker color
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));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
if (i == 4) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
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) };
}
}
There was a five different marker you can see that in the coding.Every marker was point out to a same location.My only problem is, I need to show that each and every marker have to point out different location.
Upvotes: 1
Views: 2523
Reputation: 47807
Now try this way
double latitude[] ={13.07891,13.11602,13.11355,13.12511,13.08367};
double longitude[] = {80.28215,80.23166,80.29613,80.29554,80.23961};
// lets place some 5 markers
for (int i = 0; i < 5; i++) {
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude[i], longitude[i]))
.title("Hello Maps " + i);
// changing marker color
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));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
if (i == 4) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
Upvotes: 2
Reputation: 11776
You can use a loop and create a new marker in each iteration and display. Please have a look at similar implementation
for (int i=0;i<yourarray.length();i++) {
Marker marker = userMap.addMarker(new MarkerOptions()
.position(lat[i], long[i])
.title("TITLE");
}
Upvotes: 0