我在其中一个导航抽屉片段中有谷歌地图,并且不想将其切换为活动。但是,为了简单地在地图上画一条线,我添加了以下代码:
lineOptions = new PolylineOptions()
.add(new LatLng(33.927085040000001, -118.39129683))
.add(new LatLng(33.927085969382027, -118.39129820011991))
.add(new LatLng(33.927081024586677, -118.39130352185116))
.add(new LatLng(33.927077084498386, -118.39130986277655))
.add(new LatLng(33.92707405365519, -118.39131496827862))
.add(new LatLng(33.927066722586623, -118.39131750469446))
.add(new LatLng(33.927068689880947, -118.39131823397425))
.add(new LatLng(33.927068030419839, -118.39131796208994))
.add(new LatLng(33.927065982966624, -118.39132090766913))
.add(new LatLng(33.927065258415212, -118.3913193654964))
.width(5)
.color(Color.RED);
line = mMap.addPolyline(lineOptions);
然而,它并没有在这两点之间添加线。我从这个问题Drawing a line/path on Google Maps得到了一些代码,并且该人说它有效。所以我不确定我哪里出错了。我在那里有硬编码值的原因是因为在我弄清楚之后,我将从JSON响应中绘制多个路由。以下是所有代码:
public class ThirdFragment extends Fragment implements OnMapReadyCallback {
View myView;
private GoogleMap mMap;
MapFragment mapFrag;
private Polyline line;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.third_layout, container, false);
return myView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mapFrag = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
//mapFrag.onResume();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(33.8031, 118.0726), new LatLng(33.9192, 118.4165))
.width(5)
.color(Color.RED));
mMap.setMyLocationEnabled(true);
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13);
mMap.animateCamera(yourLocation);
} else {
final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create();
alertDialogGPS.setTitle("Info");
alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app.");
alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert);
alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intentSettings);
alertDialogGPS.dismiss();
}
});
alertDialogGPS.show();
}
}
@Override
public void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mapFrag.getMapAsync(this);
}
else {
}
}
}
答案 0 :(得分:1)
我认为添加列表不是最好的方法来调用... 直接来自官方指南:
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)); // Closes the polyline.
// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);
如您所见,使用add函数添加每个点。关于long的大小,你可以把你想要的任何东西,gmaps将它想要的小数点(大约第5个数字)。试试这个,然后尝试更改坐标。 记住Lat是Y轴(非洲是〜负lat,值为-90到90),Lon是X轴(USA是负lon,值从-180到180)。