gpt4 book ai didi

java - Google Maps API groundoverlay 在应用程序中不起作用

转载 作者:行者123 更新时间:2023-11-29 19:44:53 25 4
gpt4 key购买 nike

对于我正在开发的应用程序,我想制作一个图像叠加层。在 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>

我使用的代码有问题还是我使用错误?提前致谢。

最佳答案

问题是您定义的 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();

关于java - Google Maps API groundoverlay 在应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37989807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com