【问题标题】:Using Google map within a fragment在片段中使用谷歌地图
【发布时间】:2016-12-16 20:01:29
【问题描述】:

我正在尝试在我正在处理的项目中使用 Google 地图,但无法将地图作为片段工作。我想将地图用作片段并将片段添加到我的主要活动中(假设我可以在需要时在其他地方重用此片段)但应用程序要么崩溃,要么除了加载地图之外无法执行任何操作,具体取决于关于我尝试和改变的内容。

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

这是我用来尝试从我的activity_main.xml 中显示地图的基本标签

<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,地图将被加载,但它似乎没有调用我的 Fragment 类中的任何方法。它当然不会调用 onCreate、onCreateView 或 onConnected。

这是我的片段类的代码,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="[fully qualified path name]" 因为我认为我需要一种将类和片段联系在一起的方法,所以当片段膨胀时代码将被执行。如果我这样做,我的类代码实际上会被调用,但是在 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)

我一直在尝试不同的事情,并试图了解究竟是什么导致了我的问题以及如何让它发挥作用,但无济于事。如果这不是我想做的事情的正确方法,我应该怎么做?

我尝试在此站点上查看具有类似问题的问题,但似乎没有一个问题适合我。

感谢您的帮助

【问题讨论】:

    标签: java android google-maps android-fragments


    【解决方案1】:

    我所做的是创建另一个 xml 仅用于加载地图布局 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" />
    

    然后在我的片段中膨胀该地图

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

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

    【讨论】:

    • 我会在有更多时间时尝试测试一下。我没有忘记标记答案,我最近一直全神贯注>
    【解决方案2】:

    在 Activity 的 onCreate 中添加这段代码

        FragmentManager myFragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) myFragmentManager
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    

    【讨论】:

    • 我无法从我的活动中调用它,因为我没有在活动中实现 OnMapReadyCallback。我在片段中实现,并希望我可以将所有需要的代码放入片段中,这样我就不需要将它放入任何我想拥有地图的活动中。
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多