我的Google导航片段内有谷歌地图,不想将其切换为活动。然而,仅仅简单地在地图上绘制一条线,我添加以下代码位:未在Google地图上绘制线条
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);
但是,它不加入这两个点之间的连线。我从这个SO问题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 {
}
}
}
该代码似乎是确定的,也许你不能看到由于相机变焦的线?你确定如果缩小到世界视野,你看不到线?更重要的是,你确定调用了代码(调试模式)吗? –
即使我缩小,我仍然没有看到它。无论如何,线路开始在我目前的位置。应该调用代码,因为调用了'line = mMap.addPolyline'之后的所有内容。 –
我觉得很愚蠢,我使用了Google坐标系,并没有想到在118之前放置了一个 - 符号。 –