h4kl0rd
h4kl0rd

Reputation: 625

Android - Multiple markers in google maps

Im working on an android project using java, which contains a mapping feature. My problem is how to add multiple markers(two different markers) with different colors(red, blue).

I have used the google maps api. Any assistance is appreciated.

Upvotes: 1

Views: 4058

Answers (1)

GrIsHu
GrIsHu

Reputation: 23638

Try out this way:

private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995);
private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);


private void addMarkersToMap() {
    // Uses a colored icon.
    mBrisbane = mMap.addMarker(new MarkerOptions()
            .position(BRISBANE)
            .title("Brisbane")
            .snippet("Population: 2,074,200")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    // Uses a custom icon.
    mSydney = mMap.addMarker(new MarkerOptions()
            .position(SYDNEY)
            .title("Sydney")
            .snippet("Population: 4,627,300")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
    // Creates a draggable marker. Long press to drag.
    mMelbourne = mMap.addMarker(new MarkerOptions()
            .position(MELBOURNE)
            .title("Melbourne")
            .snippet("Population: 4,137,400")
            .draggable(true));
    // A few more markers for good measure.
    mPerth = mMap.addMarker(new MarkerOptions()
            .position(PERTH)
            .title("Perth")
            .snippet("Population: 1,738,800"));
    mAdelaide = mMap.addMarker(new MarkerOptions()
            .position(ADELAIDE)
            .title("Adelaide")
            .snippet("Population: 1,213,000"));
    // Creates a marker rainbow demonstrating how to create default marker icons of different
    // hues (colors).
    int numMarkersInRainbow = 12;
    for (int i = 0; i < numMarkersInRainbow; i++) {
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(
                        -30 + 10 * Math.sin(i * Math.PI / (numMarkersInRainbow - 1)),
                        135 - 10 * Math.cos(i * Math.PI / (numMarkersInRainbow - 1))))
                .title("Marker " + i)
                .icon(BitmapDescriptorFactory.defaultMarker(i * 360 / numMarkersInRainbow)));
    }
}

Upvotes: 1

Related Questions