Google Maps API地面覆盖无法在应用中使用

时间:2016-06-23 11:05:00

标签: java android xml google-maps

对于我正在处理的应用,我想制作图像叠加层。在iOS上,我使用了Google Maps API,效果很好。但是使用完全相同的设置,配置和API,它在Android上不起作用。这是我得到的结果: current result

这显然不是我希望看到的,因为我的图像是方形的,并且应该根据坐标显得较低

我使用以下代码:

    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)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="xxx.xxx.MapFragment">

   <fragment
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</FrameLayout>

我正在使用的代码中有什么问题,或者我使用它错了吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

问题是您定义的LatLngBounds不正确。如文档中所述,LatLngBounds的构造函数需要西南和东北,并且您正在交换坐标(东南和西北)。

要解决此问题,请将LatLngBounds更改为

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();