Java 谷歌地图API地面覆盖不';在应用程序中无法工作
对于我正在开发的应用程序,我想制作一个图像覆盖。在iOS上,我使用了GoogleMapsAPI,这非常有效。但在相同的设置、配置和API下,它无法在Android上运行。这就是我得到的结果: 这显然不是我希望看到的,因为我的图像是正方形的,根据坐标应该看起来更低 我使用以下代码:Java 谷歌地图API地面覆盖不';在应用程序中无法工作,java,android,xml,google-maps,Java,Android,Xml,Google Maps,对于我正在开发的应用程序,我想制作一个图像覆盖。在iOS上,我使用了GoogleMapsAPI,这非常有效。但在相同的设置、配置和API下,它无法在Android上运行。这就是我得到的结果: 这显然不是我希望看到的,因为我的图像是正方形的,根据坐标应该看起来更低 我使用以下代码: public class MapFragment extends Fragment implements OnMapReadyCallback { LatLng lastValidTarget;
public class MapFragment extends Fragment implements OnMapReadyCallback {
LatLng lastValidTarget;
public MapFragment() {
// Required empty public constructor
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.filler_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_filler) {
System.out.println("filler");
return true;
}
return false;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View fragmentView = inflater.inflate(R.layout.fragment_map, container, false);
return fragmentView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
com.google.android.gms.maps.SupportMapFragment fragment = (com.google.android.gms.maps.SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
LatLng target = new LatLng(52.051361, 4.522754);
this.lastValidTarget = target;
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(target) // Sets the center of the map to Mountain View
.zoom(18) // Sets the zoom
.bearing(38) // Sets the orientation of the camera to east
.tilt(0) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
googleMap.setMyLocationEnabled(true);
LatLngBounds bounds = new LatLngBounds(
new LatLng(52.046518, 4.529650), // South west corner
new LatLng(52.056125, 4.516046)); // North east corner
GroundOverlay overlay = googleMap.addGroundOverlay(new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromResource(R.drawable.tester))
.positionFromBounds(bounds)
.bearing(0)
.transparency(0.3f));
}
}
以及以下内容(fragment_map.xml)
我正在使用的代码中是否有错误,或者我是否使用了错误的代码?提前感谢。问题是您定义的LatLngBounds
不正确。如文档中所述,LatLngBounds
的构造器预期为西南和东北,您正在交换坐标(东南和西北)
要解决此问题,请将youtLatLngBounds
更改为
LatLngBounds bounds = new LatLngBounds(new LatLng(52.046518, 4.516046), new LatLng(52.056125, 4.529650));
或者更好地使用LatLngBounds.Builder
来避免错误:
LatLngBounds bounds = LatLngBounds.builder()
.include(new LatLng(52.046518, 4.529650))
.include(new LatLng(52.056125, 4.516046))
.build();
LatLngBounds bounds = LatLngBounds.builder()
.include(new LatLng(52.046518, 4.529650))
.include(new LatLng(52.056125, 4.516046))
.build();