gpt4 book ai didi

java - 在 fragment 中使用 Google map

转载 作者:行者123 更新时间:2023-12-01 09:07:00 38 4
gpt4 key购买 nike

我正在尝试在我正在进行的项目中使用 Google map ,但无法让 map 作为 fragment 工作。我想使用 map 作为 fragment 并将 fragment 添加到我的主要 Activity 中(假设我可以在其他地方重用此 fragment (如果需要)),但应用程序要么崩溃,要么无法执行除加载 map 之外的任何操作,具体取决于关于我尝试和改变的事情。

这是我用作 MapsFragment.java 文件中的代码基础的教程。 https://developers.google.com/maps/documentation/android-api/current-places-tutorial

这是我用来尝试在 activity_main.xml 中显示 map 的基本标签

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="text"/>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[path info].MapsFragment"/>
</LinearLayout>

@Override
public void onConnected(Bundle connectionHint) {
getDeviceLocation();
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
}

使用此 XML, map 将加载,但它似乎没有调用我的 Fragment 类中的任何方法。它当然不会调用 onCreate、onCreateView 或 onConnected。

这是我的 fragment 类的代码,MapsFragment.java

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.LocationListener;

import java.util.Date;

public class MapsFragment extends Fragment implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {

private static final String TAG = MapsActivity.class.getSimpleName();
private GoogleMap mMap;

// A default location (Sydney, Australia) and default zoom to use when location permission is
// not granted.
private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
private static final int DEFAULT_ZOOM = 15;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private boolean mLocationPermissionGranted;


private Location mCurrentLocation;
private Marker mCurrentLocationMarker;

private CameraPosition mCameraPosition;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;

// Keys for storing activity state.
private static final String KEY_CAMERA_POSITION = "camera_position";
private static final String KEY_LOCATION = "location";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize GoogleAPIClient
Activity parentActivity = this.getActivity();
if (parentActivity != null) {
mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();

}
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_maps, container, false);
}

@Override
public void onResume() {
super.onResume();
//setUpMapIfNeeded();
if (mGoogleApiClient.isConnected()) {
getDeviceLocation();
} else {
mGoogleApiClient.connect();
}
}

@Override
public void onPause() {
super.onPause();
}

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

if (mCurrentLocation != null) {
LatLng latLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); // maybe add default zoom?
mMap.addMarker(new MarkerOptions().position(latLng).title("You")); //(latLng).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));
} else {
Log.d(TAG, "Current location is null. Using defaults.");
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, 10)); //zoom to Sydney, Australia
}
}

@Override
public void onConnected(Bundle connectionHint) {
getDeviceLocation();
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
// Refer to the reference doc for ConnectionResult to see what error codes might
// be returned in onConnectionFailed.
Log.d(TAG, "Play services connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}

@Override
public void onConnectionSuspended(int cause) {
Log.d(TAG, "Play services connection suspended");
}

@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
//updateMarkers();
if (mCurrentLocationMarker != null) {
mCurrentLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
// DEBUG
Date date = new Date();
int hour = date.getHours();
int minutes = date.getMinutes();
String time = String.valueOf(hour) + ":" + String.valueOf(minutes);
//
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));
// might need mMap.animateCamera(CameraUpdateFactory.zoomTo(11)) to animate transition. Not sure.
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(time);
mCurrentLocationMarker = mMap.addMarker(markerOptions);

Log.d(TAG, latLng.toString() + ", Time: " + time);
}

private void getDeviceLocation() {
if (ContextCompat.checkSelfPermission(this.getContext(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this.getActivity(),
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}

if (mLocationPermissionGranted) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
mLocationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
}
}
}
updateLocationUI();
}

@SuppressWarnings("MissingPermission")
private void updateLocationUI() {
if (mMap == null) {
return;
}

if (mLocationPermissionGranted) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mCurrentLocation = null;
}
}

private void createLocationRequest() {
mLocationRequest = new LocationRequest();

mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}

我尝试摆脱 android:name 名称-值对,并在 XML 中添加 class="[完全限定路径名]"因为我认为我需要一种方法将类和 fragment 绑定(bind)在一起,以便当 fragment 膨胀时,代码将被执行。如果我这样做,我的类代码实际上被调用,但是当在 onConnected 中调用 getMapAsync 时,我收到以下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at [the path info].MapsFragment.onConnected(MapsFragment.java:163)
at com.google.android.gms.common.internal.zzm.zzq(Unknown Source)
at com.google.android.gms.internal.zzaal.zzo(Unknown Source)
at com.google.android.gms.internal.zzaaj.zzvE(Unknown Source)
at com.google.android.gms.internal.zzaaj.onConnected(Unknown Source)
at com.google.android.gms.internal.zzaan.onConnected(Unknown Source)
at com.google.android.gms.internal.zzzy.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzf$zzj.zzwZ(Unknown Source)
at com.google.android.gms.common.internal.zzf$zza.zzc(Unknown Source)
at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source)
at com.google.android.gms.common.internal.zzf$zze.zzxa(Unknown Source)
at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我一直在尝试不同的事情,并试图了解到底是什么导致了我的问题以及如何使其正常工作,但无济于事。如果这不是我想做的事情的正确方法,我应该做什么?

我尝试在该网站上查找听起来类似的问题,但似乎没有一个能为我提供答案。

感谢您的帮助

最佳答案

我所做的是创建另一个 xml,仅用于加载 map 布局 xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.myApp.Fragment.MapFragment" />

之后在我的 fragment 中膨胀该 map

SupportMapFragment mapFragment;

if (mapFragment == null) {
mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));


// Check if we were successful in obtaining the map.
if (mapFragment != null) {
mapFragment.getMapAsync(this);

if (mapFragment == null) {

Toast.makeText(getContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}

启动时连接您的 api

@Override
public void onStart() {
super.onStart();
googleApiClient.connect();
}

初始化 mapFragment 后,在 onCreateView 中执行以下几行

googleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();

如果您发现任何问题请告诉我

关于java - 在 fragment 中使用 Google map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41191683/

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