Kumar Kalluri
Kumar Kalluri

Reputation: 513

code to add markers to map using android google maps v2

I have lat&long values from database.how to display markers based on lat &long values using android google map api v2.In original android google maps,we display markers based on concept itemoverlay. In v2,I dont know how to display markers.

    dbAdapter.open();
        Cursor points = dbAdapter.clustercall(Btmlft_latitude, toprgt_latitude,
                Btmlft_longitude, toprgt_longitude, gridsize1);
        int c = points.getCount();
        Log.d("count of points", "" + c);

        if (points != null) {
            if (points.moveToFirst()) {
                do {

                    int latitude = (int) (points.getFloat(points
                            .getColumnIndex("LATITUDE")) * 1E6);
                    int longitude = (int) (points.getFloat(points
                            .getColumnIndex("LONGITUDE")) * 1E6);

                    mapView.addMarker(new MarkerOptions().position(
                            new LatLng(latitude, longitude)).icon(
                            BitmapDescriptorFactory.defaultMarker()));

                } while (points.moveToNext());

            }
        }
        points.close();
        dbAdapter.close();

That is mycode.I am getting lat&long values from database,but how to add markers based on lat &long values to map.

I read the android google maps api v2.In that have only give statically added data of markers

Upvotes: 14

Views: 81541

Answers (3)

dumazy
dumazy

Reputation: 14445

You have to use the method GoogleMap.addMarker(MarkerOptions); I've explained this in a tutorial on my blog: http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html

On this blog, you'll find some more posts concerning the new Maps API:

  • Move the map to current or some location
  • Create the custom InfoWindow
  • Associate geodata or objects to a marker

Upvotes: 12

K_Anas
K_Anas

Reputation: 31456

Use the Marker Class

An icon placed at a particular point on the map's surface. A marker icon is drawn oriented against the device's screen rather than the map's surface; i.e., it will not necessarily change orientation due to map rotations, tilting, or zooming.

A marker has the following properties:

Anchor

The point on the image that will be placed at the LatLng position of the marker. This defaults to 50% from the left of the image and at the bottom of the image.

Position

The LatLng value for the marker's position on the map. You can change this value at any time if you want to move the marker.

Title

A text string that's displayed in an info window when the user taps the marker. You can change this value at any time.

Snippet

Additional text that's displayed below the title. You can change this value at any time.

Icon

A bitmap that's displayed for the marker. If the icon is left unset, a default icon is displayed. You can specify an alternative coloring of the default icon using defaultMarker(float). You can't change the icon once you've created the marker.

Drag Status

If you want to allow the user to drag the marker, set this property to true. You can change this value at any time. The default is true.

Visibility

By default, the marker is visible. To make the marker invisible, set this property to false. You can change this value at any time.

GoogleMap map = ... // get a map.
   // Add a marker at San Francisco.
   Marker marker = map.addMarker(new MarkerOptions()
       .position(new LatLng(37.7750, 122.4183))
       .title("San Francisco")
       .snippet("Population: 776733"));

Upvotes: 27

DiscDev
DiscDev

Reputation: 39052

I'm guessing the issue is that you are multiplying your latitude/longitude values by 1E6 instead of dividing them by 1E6. If your stored values came from GeoPoints, that might be your issue. Otherwise, the way you are adding the Markers is fine and should work.

i.e. the lat/lng values you pass to new LatLng() should look like 45.4,-95.5 NOT 45400000,-95500000

Check out the following code:

//mMap is a GoogleMap and point is a GeoPoint
this.mMap.addMarker(getMarker(point, drawableId))

...

public MarkerOptions getMarker(GeoPoint point, int drawableId)
{
    return new MarkerOptions()
    .position(new LatLng(point.getLatitudeE6() / 1000000.0, point.getLongitudeE6() / 1000000.0))
    .icon(BitmapDescriptorFactory.fromResource(drawableId));
}

Upvotes: 4

Related Questions