我的应用程序应在第一个片段中显示地图。该应用程序由导航抽屉组织。该地图在没有问题的情况下显示出来,但没有显示我提供的默认缩放和位置。
我创建应用程序时遇到的第一个错误是NullPointerException
。我知道如何修复它,所以我说我的地图碎片(mapFragment
(不是零。该应用现在开始工作,但是我在方法onMapReady
中编写的代码以及以获取当前位置并将相机移动到此特定位置的按钮不起作用。
我还尝试更改地图的代码,但我总是在同一地点。如果我不写map!=null
,我会得到NullPointerException
。如果我确实写了它,则该应用程序不会正确放大到特定位置。
在Mainactivity
中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.mapFragment);
if(mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
这是我必须防止NullPointerException
(mapFragment(
在这里,我设置了默认位置和Zoom,该位置并未应用于地图上(在具有mapFragment!= null
时(:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
{
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
LatLng Luxembourg = new LatLng(49.611622, 6.131935);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(Luxembourg, 14));
}
这是检索用户当前位置的方法(在拥有mapfragment!= null时也无法工作(:
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
if(currentUserLocationMarker != null)
{
currentUserLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("current user location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker
(BitmapDescriptorFactory.HUE_RED));
currentUserLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(14));
if(googleApiClient != null)
{
LocationServices.FusedLocationApi.removeLocationUpdates
(googleApiClient, this);
}
}
这是mapragment本身:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</FrameLayout>
我希望该地图具有我提供的默认位置和缩放以及将地图移至用户当前位置的按钮。
您可以考虑为SupportMapFragment
编写构造函数,然后在进行片段交易之前先初始化它。因此,您将无法获得NullPointerException
。
我建议以下内容。
// Declare this in your class as a public variable.
public SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapFragment = new SupportMapFragment();
// I suppose this is doing something that requires the context.
// Omit this for now
// mapFragment.getMapAsync(this);
// Do the fragment transaction here
}
然后,在SupportMapFragment
的onResume
方法中,请执行以下操作。
@Override
public void onResume() {
super.onResume();
getMapAsync(getActivity());
}
这只是示例代码。您可以考虑将参数传递给SupportMapFragment
的构造函数,这将初始化所需的变量。我希望你明白这个主意。