片段中的当前位置

时间:2016-05-17 14:01:00

标签: android google-maps android-fragments currentlocation

我尝试了很多来源,但我没有得到我的片段应该用于获取当前位置的内容。 这是我的片段:

 public class GMapFragment extends Fragment implements OnMapReadyCallback {
    private GoogleMap googleMap;
    MapFragment fragment;


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

            View rootView = inflater.inflate(R.layout.fragment_gmap, container, false);
            initilizeMap();
            return rootView;
        }
         private void initilizeMap()
        {
            MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
               if (fragment != null){
                   fragment.getMapAsync(this);

               }

        }
        @Override
        public void onMapReady(final GoogleMap googleMap) {
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(true);

        }

         @Override
        public void onPause() {

            final FragmentManager fragManager = this.getFragmentManager();
            final Fragment fragment = fragManager.findFragmentById(R.id.map);
            if(fragment!=null){
                fragManager.beginTransaction().remove(fragment).commit();
            super.onPause();

            }
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            final FragmentManager fragManager = this.getFragmentManager();
            final Fragment fragment = fragManager.findFragmentById(R.id.map);
            if(fragment!=null){
                fragManager.beginTransaction().remove(fragment).commit();
            }
        }
        }

如果有人可以提供帮助?我需要一个可以与此片段一起使用的源或样本。请帮忙,我在这里呆了几天

3 个答案:

答案 0 :(得分:0)

您的清单文件中是否有ACCESS_FINE_LOCATION?

此外,请注意,如果您的应用在> = API 23设备上运行,则必须向用户请求权限授予。

关注官方GUIDE

编辑:

您是否初步确定了Google Play服务?

答案 1 :(得分:0)

来自文档

  

启用后,位置可用

您有可用的位置吗?

答案 2 :(得分:0)

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

在onStart上连接上述google api客户端,并在onstop

中断开连接
@Override
    public void onStart() {
        super.onStart();
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }

覆盖方法。

public void onConnected(@Nullable Bundle bundle) {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
        }
        myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (myLocation != null) {
            showCurrentLocation(myLocation);
            return;
        }
        if (!NetworkHelper.isGpsEnabled(getActivity()))
            PopUp.showAlertDialog(act, "Enable GPS",
                    "Help us to get your current location");
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, locationRequest, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        if (location == null)
                            return;
                        myLocation = location;
                        showCurrentLocation(location);
                    }
                });


    }