Java 在片段中而不是在活动中集成映射

Java 在片段中而不是在活动中集成映射,java,android,google-maps,android-fragments,Java,Android,Google Maps,Android Fragments,我想把谷歌地图添加到主活动中的一个片段中。 但我在下面的陈述中发现了一个错误 此fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此) getApplicationContext() Toast.makeText(getApplicationContext(),currentLocation.getLatitude() getSupportFragmentManager()getSuppor

我想把谷歌地图添加到主活动中的一个片段中。 但我在下面的陈述中发现了一个错误

  • fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此)

  • getApplicationContext()
    Toast.makeText(getApplicationContext(),currentLocation.getLatitude()

  • getSupportFragmentManager()
    getSupportFragmentManager().findFragmentById(R.id.google_-map);
    相同的代码在活动中工作。由于这是在片段中编码的,应该做哪些更改

  • 导入android.content.pm.PackageManager;
    导入android.location.location;
    导入android.os.Bundle;
    导入android.view.LayoutInflater;
    导入android.view.view;
    导入android.view.ViewGroup;
    导入android.widget.Toast;
    导入androidx.annotation.NonNull;
    导入androidx.annotation.Nullable;
    导入androidx.core.app.ActivityCompat;
    导入androidx.fragment.app.fragment;
    导入com.google.android.gms.location.FusedLocationProviderClient;
    导入com.google.android.gms.location.LocationServices;
    导入com.google.android.gms.maps.CameraUpdateFactory;
    导入com.google.android.gms.maps.GoogleMap;
    导入com.google.android.gms.maps.OnMapReadyCallback;
    导入com.google.android.gms.maps.SupportMapFragment;
    导入com.google.android.gms.maps.model.LatLng;
    导入com.google.android.gms.maps.model.MarkerOptions;
    导入com.google.android.gms.tasks.OnSuccessListener;
    导入com.google.android.gms.tasks.Task;
    公共类MapFragment扩展了MapReadyCallback上的片段实现{
    位置当前位置;
    FusedLocationProviderClient FusedLocationProviderClient;
    私有静态最终整数请求_代码=101;
    @可空
    @凌驾
    创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
    返回充气机。充气(R.layout.fragment_图,容器,假);
    fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此);
    fetchLastLocation();
    }
    私有void fetchLastLocation(){
    如果(ActivityCompat.checkSelfPermission)(getActivity(),
    Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予&&
    ActivityCompat.checkSelfPermission(getActivity(),
    清单.权限.访问(位置)=
    PackageManager.权限(已授予){
    ActivityCompat.requestPermissions(getActivity(),新字符串[]
    {Manifest.permission.ACCESS\u FINE\u LOCATION},请求\u代码);
    返回;
    }
    Task Task=fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(新的OnSuccessListener(){
    @凌驾
    成功时的公共无效(位置){
    如果(位置!=null){
    当前位置=位置;
    Toast.makeText(getApplicationContext(),currentLocation.getLatitude())
    +“”+currentLocation.getLength(),Toast.LENGTH_SHORT.show();
    SupportMapFragment SupportMapFragment=(SupportMapFragment)
    getSupportFragmentManager();
    supportMapFragment.getMapAsync(MapFragment.this);
    }
    }
    });
    }
    @凌驾
    4月1日公开作废(谷歌地图谷歌地图){
    LatLng LatLng=新LatLng(currentLocation.getLatitude(),currentLocation.getLength());
    MarkerOptions MarkerOptions=newmarkeroptions().position(latLng.title)(“我在这里”);
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,13));
    googleMap.addMarker(markerOptions);
    }
    @凌驾
    public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
    开关(请求代码){
    案例请求代码:
    如果(grantResults.length>0&&grantResults[0]==已授予PackageManager.PERMISSION){
    fetchLastLocation();
    }
    打破
    }
    }
    }
    
    尝试在此处使用
    getActivity()
    而不是
    this

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    
    并使用
    getActivity()
    而不是
    getApplicationContext()

    第三个问题
    getActivity().getSupportFragmentManager()

    更新

    试一试


    我找到了这个答案。任何在这方面遇到麻烦的人都可以使用以下方法: 而不是

    return inflater.inflate(R.layout.fragment_map, container, false);
    
    我编码为

    View v = inflater.inflate(R.layout.fragment_map, container, false); 
    return v;
    
    然后

  • 使用
    getActivity()
  • 使用
    v.getApplicationContext()代替
    getApplicationContext()
  • 使用
    v.getSupportFragmentManager()

  • 您遇到了什么错误?将错误日志与question@akhilesh0707在片段中可以使用什么来代替“activity:this?”请尝试使用getActivity()代替此处的“this”-这不起作用。它显示“无法访问的语句”。2.getActivity().getApplicationContext()3.getActivity().getSupportFragmentManager()我在第二次和第三次尝试了这种方法,效果很好。谢谢。但是第一条语句仍然用红色下划线。您是这样做的:
    fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(getActivity());
    return inflater.inflate(R.layout.fragment_map, container, false);
    
    View v = inflater.inflate(R.layout.fragment_map, container, false); 
    return v;