在当前位置

时间:2016-03-18 13:36:59

标签: android google-maps location

我一直试图让我的地图片段在用户的当前位置打开,但我不知道该怎么做...我已经尝试了很多我在很多网站上搜索的东西,但我只能按myLocationButton时居中当前位置。

我的MapFragment.java:

    package com.example.alex.testing_map;

    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.content.ContextCompat;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;


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


 public class MapFragment extends Fragment implements OnMapReadyCallback{


private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment, viewGroup, false);
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    SupportMapFragment fragment = new SupportMapFragment();
    transaction.replace(R.id.mapView, fragment);
    transaction.commit();

    fragment.getMapAsync(this);
    return view;
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);

        } else {
        }
    }

    mMap.getUiSettings().setCompassEnabled(false);
    mMap.getUiSettings().setIndoorLevelPickerEnabled(false);
    mMap.getUiSettings().setMapToolbarEnabled(false);
    mMap.getUiSettings().setRotateGesturesEnabled(false);
    mMap.getUiSettings().setZoomControlsEnabled(false);



    LatLng p1 = new LatLng(41.39355, 2.15473);
    mMap.addMarker(new MarkerOptions().position(p1).icon(BitmapDescriptorFactory.fromResource(R.drawable.pointer_bici1)));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(p1, 18));


}

}

目前地图在点p1打开。

非常感谢提前!!

1 个答案:

答案 0 :(得分:2)

您需要将onMyLocationChangeListener设置为地图。

而不是

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(p1, 18));

使用它:

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 18);
        mMap.animateCamera(cu);
    }
});

或者,在Kotlin中,使用:

mMap.setOnMyLocationChangeListener { location ->
    val cu = CameraUpdateFactory.newLatLngZoom(LatLng(location.getLatitude(), location.getLongitude()), 18f)
    mMap.animateCamera(cu)
}