首页 > 解决方案 > 折线不会出现

问题描述

折线不会绘制

我有一个坐标数组,我想从中绘制一条折线,但是当我这样做时……什么都没有出现。我试图调整缩放级别,但都是徒劳的。实际上,数组是从 UI 填充的

private void mDrawLine(ArrayList<LatLng> selected_cordinates) {
        int length = selected_cordinates.size();


        if (length >=1 && length <=3){

            for (int j = 0 ;j < length-1; j++){

                PolylineOptions options = new PolylineOptions();

                if (selected_cordinates.get(j) != null && selected_cordinates.get(j) != null){


                    //LatLng latLng = new LatLng( selected_cordinates.get(i).latitude,selected_cordinates.get(i).longitude);
                    options.add(new LatLng( selected_cordinates.get(j).latitude,selected_cordinates.get(j).longitude));
                    options.color(Color.BLUE);
                    options.width(5);

                    line = mGoogleMap.addPolyline(options);

                }
            }

            Toast.makeText(this, "Your Saved Values"+selected_cordinates, Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "Polygon Has Been Drawn", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(this,"You can only Draw a line with 3 Points",Toast.LENGTH_LONG).show();
        }

    }

// 这就是我将坐标存储到数组中的方式

public void savePoints(){

            Double  mLat_value ;
            Double  mLng_value ;
            float   mAcc;

            //float mLatlng_accuracy;

            String nLat_value = mtextLat.getText().toString();
            String nLng_value = mtextLng.getText().toString();
            String nAcc =  mtextAccuracy.getText().toString();

            mLat_value = Double.parseDouble(nLat_value);
            mLng_value = Double.parseDouble(nLng_value);

            selected_cordinates.add(new LatLng(mLat_value,mLng_value));
    }

谷歌地图上的折线。

标签: javagoogle-maps

解决方案


希望这段代码能解决你的问题

var DirectionsService = 新的 google.maps.DirectionsService(); var directionDisplay = new google.maps.DirectionsRenderer({ map: map, preserveViewport: true }); // console.log("Lastdata : ", lastdata); // console.log("perulangan maksimal : ", perulanganmaksimal); 路点 = []; for (var x = 1; x < obj.length -1; x++) { waypts.push({ location: new google.maps.LatLng(obj[x].lnglat_pecah[0], obj[x].lnglat_pecah[1] ]), 中途停留: false }); // console.log("x : ", x); }

directionsService.route({
  origin: new google.maps.LatLng(obj[0].lnglat_pecah[0], obj[0].lnglat_pecah[1]),
  destination: new google.maps.LatLng(lastdata.lnglat_pecah[0], lastdata.lnglat_pecah[1]),
    waypoints: waypts,
    travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
  if (status === google.maps.DirectionsStatus.OK) {
     symbol = {
       path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
       // path : 'M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z',
       scale: 3,
       strokeColor: '#F00'
     };
     polyline = new google.maps.Polyline({
      path: [],
      strokeColor: '#0000FF',
      strokeWeight: 3,
       geodesic: geodesicvalue,
      icons: [{
        icon: symbol,
        offset: '100%',
        repeat: repeated
       }]
    });
    var bounds = new google.maps.LatLngBounds();
    // console.log("polyline", polyline);

    var legs = response.routes[0].legs;
    // console.log("legs", legs);
    for (ulang = 0; ulang < legs.length; ulang++) {
      var steps = legs[ulang].steps;
      for (j = 0; j < steps.length; j++) {
        var nextSegment = steps[j].path;
        for (k = 0; k < nextSegment.length; k++) {
          polyline.getPath().push(nextSegment[k]);
          bounds.extend(nextSegment[k]);
        }
      }
    }

    polyline.setMap(map);

推荐阅读