在MapsView中使用getMapAsync()时,应用崩溃 [英] App crash when using getMapAsync() in MapsView

查看:97
本文介绍了在MapsView中使用getMapAsync()时,应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mapsview,使用getMap()时可以正常工作,尽管它向我显示了警告.但是,当我将getMap()更改为getMapAsync()时,应用程序将崩溃.

I have a mapsview and it works fine when using getMap() although it shows me a depricated warning. But when i change getMap() to getMapAsync() the app will crash.

这是我的代码

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMapAsync(this);

        mMap.setOnInfoWindowClickListener(this);
        mMap.setOnInfoWindowCloseListener(this);
        mMap.setOnMarkerDragListener(this);

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                markerPoints.add(latLng);
                MarkerOptions options = new MarkerOptions();
                // Setting the position of the marker
                options.position(latLng);
            }
        });

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {    
                //Getting the coordinates
                toLatitude = marker.getPosition().latitude;
                toLongitude = marker.getPosition().longitude;

                dest = new LatLng(toLatitude, toLongitude);
                mMap.animateCamera(CameraUpdateFactory.newLatLng(dest));

                return ((toLatitude == my_marker.getPosition().latitude) && (toLongitude == my_marker.getPosition().longitude));
            }
        });
    }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        setUpMap();
    }

    private void setUpMap() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            Toast.makeText(MapsActivity.this, "Anda harus menyetujuinya agar dapat menikmati semua fitur yang ada", Toast.LENGTH_LONG).show();
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            }
        }

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setTrafficEnabled(true);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .zoom(15)                   // Sets the zoom
                .target(new LatLng(-6.597629,106.79957))
                .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder
        mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
}

这是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=".MapsActivity">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

这是清单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我的代码有什么问题?任何帮助将不胜感激.

What's wrong with my code? Any help will be greatly appreciated.

谢谢.

推荐答案

尝试一下,

SupportMapFragment mapFragment =  ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));

    if(mapFragment != null) {
       mapFragment.getMapAsync(this)
    }

并移动代码

mMap.setOnInfoWindowClickListener(this);
    mMap.setOnInfoWindowCloseListener(this);
    mMap.setOnMarkerDragListener(this);

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            markerPoints.add(latLng);
            MarkerOptions options = new MarkerOptions();
            // Setting the position of the marker
            options.position(latLng);
        }
    });

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {    
            //Getting the coordinates
            toLatitude = marker.getPosition().latitude;
            toLongitude = marker.getPosition().longitude;

            dest = new LatLng(toLatitude, toLongitude);
            mMap.animateCamera(CameraUpdateFactory.newLatLng(dest));

            return ((toLatitude == my_marker.getPosition().latitude) && (toLongitude == my_marker.getPosition().longitude));
        }
    });
}

要setUpMap()方法,我在这里尝试过.它的工作.请检查一次

to setUpMap() method, i tried here. its working. check once please

这篇关于在MapsView中使用getMapAsync()时,应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆