在我的应用程序中,我添加了两个地方自动完成片段作为方向的源和目标搜索框。我已经添加了两个标记,但它只显示地图上方框中写的位置。我尝试添加折线,但它不起作用。我只添加了谷歌地图,位置和地点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但我不知道如何在它们之间获得折线
}
}
答案 0 :(得分:0)
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);