【问题标题】:How to show polyline between two markers?如何在两个标记之间显示折线?
【发布时间】:2023-06-29 15:31:01
【问题描述】:

在我的应用程序中,我添加了两个位置自动完成片段作为方向的源和目标搜索框。我在上面添加了两个标记,但它只显示了地图上方框中写的位置。我尝试添加折线,但它不起作用。我只添加了谷歌地图、位置和地点 API。

 PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);


        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                addMarker(place);
                // Log.i(TAG, "Place: " + place.getName());
                String placeName = place.getName().toString();
                place.getLatLng();


            }

            @Override
            public void onError(Status status) {                       
            }
        });

        PlaceAutocompleteFragment autocompleteFragments = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragments);
        autocompleteFragments.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place places) {                  
                addMarker(place);//used to add marker according to the given string                       
                String placeName = places.getName().toString();
                places.getLatLng();


            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                // Log.i(TAG, "An error occurred: " + status);

            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;               
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {                

            return;
        }
        mMap.setMyLocationEnabled(true);
    }

    public void addMarker(Place p){
        MarkerOptions markerOptions=new MarkerOptions();
        markerOptions.position(p.getLatLng()).title(p.getName()+"");
        mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(p.getLatLng()));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
        PolylineOptions polylineOptions=new PolylineOptions();

//尝试添加折线但没有成功

   polylineOptions.add(p.getLatLng()).width(5).color(Color.BLUE).geodesic(true);
        mMap.addPolyline(polylineOptions);

//p.getLatLng() 这是用来获取 latlng 但我不知道如何获取它们之间的折线

    }
    } 

【问题讨论】:

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


【解决方案1】:
ArrayList<LatLng> list = getArrayListOfLatLng();
PolyLine line;

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int z = 0; z < list.size(); z++) {
        LatLng point = list.get(z);
        options.add(point);
    }
    line = myMap.addPolyline(options);

【讨论】:

  • 我遇到了错误。我没有定义列表或行。我应该添加什么列表
  • 请在您的代码周围添加一些注释。代码本身不足以提供答案。
  • 这里 line 表示折线,list 表示 LatLng ArrayList 如果对我有帮助,请投票否则评论我会尽力帮助你 这里是 gmaps 的折线链接*.com/questions/17425499/…