Android Google Maps API V2 Zoom to Current Location

前端 未结 9 1716
时光说笑
时光说笑 2020-11-28 04:37

I\'m trying to mess around with the Maps API V2 to get more familiar with it, and I\'m trying to start the map centered at the user\'s current location. Using the map.

相关标签:
9条回答
  • 2020-11-28 05:35

    try this code :

    private GoogleMap mMap;
    
    
    LocationManager locationManager;
    
    
    private static final String TAG = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);
    
        arrayPoints = new ArrayList<LatLng>();
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
    
    
        mMap = googleMap;
    
    
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    
    
        LatLng myPosition;
    
    
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
    
    
        if (location != null) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            LatLng latLng = new LatLng(latitude, longitude);
            myPosition = new LatLng(latitude, longitude);
    
    
            LatLng coordinate = new LatLng(latitude, longitude);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 19);
            mMap.animateCamera(yourLocation);
        }
    }
    

    }

    Dont forget to add permissions on AndroidManifest.xml.

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    
    0 讨论(0)
  • 2020-11-28 05:37

    Here's how to do it inside ViewModel and FusedLocationProviderClient, code in Kotlin

    locationClient.lastLocation.addOnSuccessListener { location: Location? ->
                location?.let {
                    val position = CameraPosition.Builder()
                            .target(LatLng(it.latitude, it.longitude))
                            .zoom(15.0f)
                            .build()
                    map.animateCamera(CameraUpdateFactory.newCameraPosition(position))
                }
            }
    
    0 讨论(0)
  • 2020-11-28 05:40
        mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
    
                    CameraUpdate center=CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
                    CameraUpdate zoom=CameraUpdateFactory.zoomTo(11);
                    mMap.moveCamera(center);
                    mMap.animateCamera(zoom);
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题