gpt4 book ai didi

java - Androidx 的 fragment 中的 map 实现

转载 作者:行者123 更新时间:2023-12-01 19:14:53 30 4
gpt4 key购买 nike

该错误与 getMapAsync() 函数有关。这是导致错误的主要原因。我正在尝试在此 fragment 中实现 getMapAsync() 。错误是:

error: cannot find symbol method getMapAsync(GenHomeFragment)

我在互联网上搜索但没有找到合适的解决方案,因为这个解决方案不适用于AndroidX,所以我无法解决这个问题。

fragment 如下:

package e.user.madiambu;



import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;




import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationListener;

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.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


/**
* A simple {@link Fragment} subclass.
*/
public class GenHomeFragment extends Fragment implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener


{

private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentUserLocationMarker;

private static final int Request_User_location_code=99;


public GenHomeFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_gen_home, container, false);


if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
checkUserLocationPermission();
}
FragmentManager fm = getFragmentManager();
Fragment mapFragment = fm.findFragmentById(R.id.googleMap);

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

return view;
}



@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;


if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
{
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}

}

public boolean checkUserLocationPermission()
{
if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION},Request_User_location_code);
}
else
{
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION},Request_User_location_code);
}

return false;
}

else
{
return true;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case Request_User_location_code:
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
if (ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
{
if (googleApiClient==null)
{
buildGoogleApiClient();//Create new Client.....
}
mMap.setMyLocationEnabled(true);
}
}

else
{
Toast.makeText(getActivity(),"permission Denied...", Toast.LENGTH_SHORT).show();
}
return;
}
}


protected synchronized void buildGoogleApiClient()
{
googleApiClient=new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect(); //Connected Google api client.....
}

@Override
public void onConnected(@Nullable Bundle bundle) {

locationRequest=new LocationRequest();
locationRequest.setInterval(1100);
locationRequest.setFastestInterval(1100);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

if (ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest,this);
}

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}



@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("User Current Location");//Show user current location maker....
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
currentUserLocationMarker=mMap.addMarker(markerOptions);

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(12));

if(googleApiClient !=null)
{
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}


}
}

我的 xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<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=".GenHomeFragment">

<com.google.android.gms.maps.MapView
android:id="@+id/googleMap"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

最佳答案

com.google.android.gms.maps.MapView 不是一个 fragment ,它是一个 View 。这里您没有使用具有谷歌地图的 fragment ,例如

<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_view_map"
class="com.google.android.gms.maps.MapFragment"></fragment>

当您使用mapView时,请按照以下步骤操作:

private MapView mMap;

在onCreateView中

mMap = findViewById(R.id.googleMap)

然后你就可以使用

mMap.getMapAsync(this)

关于java - Androidx 的 fragment 中的 map 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59429158/

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