如何在Android中的谷歌地图上绘制多边形线

时间:2015-01-02 04:49:41

标签: android

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.act_chat);

}
@Override
onResume() {
    if (map != null) {
        // Enable MyLocation Button in the Map
        map.setMyLocationEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(false);
        try {
            LatLng point = new LatLng(Double.parseDouble(Latreccvier),
                    Double.parseDouble(Lonreccvier));

            BitmapDescriptor icon = BitmapDescriptorFactory
                    .fromResource(R.drawable.reccivermarker);

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(point);
            markerOptions.icon(icon);
            map.addMarker(markerOptions);

            // // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 14));
            map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
        } catch (Exception e) {
        }

    }
}

@Override
public void RealTimeLocation(int memberId, final double lat, final double lng) {
    // TODO Auto-generated method stub
    runOnUiThread(new Runnable() {
        public void run() {

            Latreccvier = String.valueOf(lat);
            Lonreccvier = String.valueOf(lng);

            LatLng point1=new LatLng(Double.parseDouble(Latreccvier), Double.parseDouble(Lonreccvier));
            friendMarker.setPosition(point1);
        }
    });
}

这是我的代码我想在谷歌地图上绘制线条我能够同时移动标记我想绘制移动谷歌地图线的路径。

public void RealTimeLocation(int memberId, final double lat, final double lng) { 
a

使用这种方法我的位置已经改变了4到5 lat我试图添加freindlatlong.add(point1); 其中freindlatlong是数组列表根据我尝试添加Plyline但不工作请建议我如何在地图上绘制路径线。

2 个答案:

答案 0 :(得分:1)

@Override
onResume() {

        if (map != null) {

            // Enable MyLocation Button in the Map

            map.setMyLocationEnabled(true);

            map.getUiSettings().setZoomControlsEnabled(false);

            try {

                LatLng point = new LatLng(Double.parseDouble(Latreccvier),
                        Double.parseDouble(Lonreccvier));

                BitmapDescriptor icon = BitmapDescriptorFactory
                        .fromResource(R.drawable.reccivermarker);

                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(point);
                markerOptions.icon(icon);
                map.addMarker(markerOptions);
                // map.addPolyline(options);
                // // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 14));
                map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
            } catch (Exception e) {
            }

        }

    }




@Override
        public void RealTimeLocation(int memberId, final double lat,
                final double lng) {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                public void run() {

                    Latreccvier = String.valueOf(lat);
                    Lonreccvier = String.valueOf(lng);

                    LatLng point1 = new LatLng(Double.parseDouble(Latreccvier),
                            Double.parseDouble(Lonreccvier));

                    cordinatelist.add(point1);

                    friendMarker.setPosition(point1);
                    // options.add(point1);


}
            });
        }

    };

请替换此代码

答案 1 :(得分:0)

试试这个非常好的绘制折线的代码:

 MarkerOptions markerOptions = new MarkerOptions();

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

            // Setting titile of the infowindow of the marker
            markerOptions.title("Position");

            // Setting the content of the infowindow of the marker
            markerOptions.snippet("Latitude:"+point.latitude+","+"Longitude:"+point.longitude);

            // Instantiating the class PolylineOptions to plot polyline in the map
            PolylineOptions polylineOptions = new PolylineOptions();

            // Setting the color of the polyline
            polylineOptions.color(Color.RED);

            // Setting the width of the polyline
            polylineOptions.width(3);

            // Adding the taped point to the ArrayList
            points.add(point);

            // Setting points of polyline
            polylineOptions.addAll(points);

            // Adding the polyline to the map
            googleMap.addPolyline(polylineOptions);

            // Adding the marker to the map
            googleMap.addMarker(markerOptions);

参考: Adding poly-line on tap