如何使用google map api v2在地图加载时在源和目标上添加标记

时间:2013-09-05 06:10:14

标签: android

      public void onLoad() {

// Opening the sharedPreferences object
preferences = getSharedPreferences("location", 0);

// Getting number of locations already stored
routeType = preferences.getInt("routeType", 0);

// Getting stored zoom level if exists else return 0
String zoom = preferences.getString("zoom", "0");

// If locations are already saved
if (routeType != 0) {

    String lat = "sourceAdd";
    String lng = "destinationAdd";

    // Iterating through all the locations stored
    for (int i = 0; i < routeType; i++) {

        // Getting the latitude of the i-th location
        lat = preferences.getString("lat" + i, "0");

        // Getting the longitude of the i-th location
        lng = preferences.getString("lng" + i, "0");

        // Drawing marker on the map
        drawMarker(new LatLng(Double.parseDouble(lat),
                Double.parseDouble(lng)));
    }
    // Moving CameraPosition to last clicked position
    map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(Double
            .parseDouble(lat), Double.parseDouble(lng))));

    // Setting the zoom level in the map on last position is clicked
    map.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));
}

}

public void drawMarker(LatLng point){

    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions = new MarkerOptions();

    // Setting latitude and longitude for the marker
    markerOptions.position(point);

    // Adding marker on the Google Map

    map.addMarker(new MarkerOptions()
            // map.addMarker(markerOptions);
            .title("Marker")
            .snippet("Marker Yo Yo")
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
            .position(point)

    );

}

您好,

我需要在地图上添加2个标记,例如源和目的地,我使用上面的代码,当我运行应用程序时,我得到一条黑线显示源和目的地之间的路线,但标记没有出现在地图上:(有人能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

试试这段代码

public void drawMarker(LatLng source_point,LatLong destination_point) {

    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions1 = new MarkerOptions();
    markerOptions1.title("Marker");
    markerOptions1.snippet("Marker Yo Yo");
    markerOptions1.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
    markerOptions1.position(source_point);

    MarkerOptions markerOptions2 = new MarkerOptions();
    markerOptions2.title("Marker2");
    markerOptions2.snippet("Marker Xo Xo");
    markerOptions2.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
    markerOptions2.position(destination_point);

    // Adding marker on the Google Map

    map.addMarker(markerOptions1);
    map.addMarker(markerOptions2);
}