如何在android中使用新坐标重绘地图标记

时间:2017-09-25 07:19:22

标签: android google-maps google-maps-api-3

我在谷歌地图上显示了一些观点。我能够在第一次加载时显示它。使用onMapReady函数。

@Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        map.setMyLocationEnabled(true);
        LatLng driver_point = new LatLng(driver_latitude, driver_longitude);
        LatLng pickup_point = new LatLng(pickup_latitude, pickup_longitude);
        LatLng drop_point = new LatLng(drop_latitude, drop_longitude);

        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        if (driver_point.latitude != 0.0)
            builder.include(driver_point);
        builder.include(pickup_point);
        builder.include(drop_point);

        LatLngBounds bounds = builder.build();

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        int padding = (int) (width * 0.10);

        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);

        map.animateCamera(cu);
        if (driver_point.latitude != 0.0)
            markerPoints.add(driver_point);
        markerPoints.add(pickup_point);
        markerPoints.add(drop_point);

        // Creating MarkerOptions
        MarkerOptions driver_opt = new MarkerOptions();
        // Setting the position of the marker
        driver_opt.position(driver_point);
        driver_opt.icon(BitmapDescriptorFactory.fromResource(R.drawable.car_tiny));
        map.addMarker(driver_opt);

        // Creating MarkerOptions
        MarkerOptions pickup_opt = new MarkerOptions();
        // Setting the position of the marker
        pickup_opt.position(pickup_point);
        pickup_opt.icon(BitmapDescriptorFactory.fromResource(R.drawable.pickup_icon));
        map.addMarker(pickup_opt);

        MarkerOptions drop_opt = new MarkerOptions();
        // Setting the position of the marker
        drop_opt.position(drop_point);
        drop_opt.icon(BitmapDescriptorFactory.fromResource(R.drawable.dropoff_icon));
        map.addMarker(drop_opt);
    }

服务器调用后,我想用new更新标记。我怎样才能做到这一点? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

创建一个函数并将新的lat lng传递给它。如果要清除以前的标记,只需调用map.clear()

    public void drawmap(LatLng newlatlng)
{
    map.clear();
    map.addMarker(new MarkerOptions().position(newlatlng).title("Marker in ??"));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(newlatlng,15.0f));
}

答案 1 :(得分:0)

您可以使用默认(红色)标记的多个标记。

LatLng coordinate1 = new LatLng(latitude1, longitude1);

CameraUpdate yourLocation1 = CameraUpdateFactory.newLatLngZoom(coordinate1,15);

mMap.animateCamera(yourLocation1);

///

LatLng coordinate2 = new LatLng(latitude2, longitude2);

CameraUpdate yourLocation2 = CameraUpdateFactory.newLatLngZoom(coordinate2,15);

mMap.animateCamera(yourLocation2);

///

LatLng coordinate3 = new LatLng(latitude3, longitude3);

CameraUpdate yourLocation3 = CameraUpdateFactory.newLatLngZoom(coordinate3,15);

mMap.animateCamera(yourLocation3);