Android Google Maps API V2 Zoom to Current Location

前端 未结 9 1763
时光说笑
时光说笑 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:27

    private void setUpMapIfNeeded(){
        if (mMap == null){
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();//invoke of map fragment by id from main xml file
    
         if (mMap != null) {
             mMap.setMyLocationEnabled(true);//Makes the users current location visible by displaying a blue dot.
    
             LocationManager lm=(LocationManager)getSystemService(LOCATION_SERVICE);//use of location services by firstly defining location manager.
             String provider=lm.getBestProvider(new Criteria(), true);
    
             if(provider==null){
                 onProviderDisabled(provider);
                  }
             Location loc=lm.getLastKnownLocation(provider);
    
    
             if (loc!=null){
                 onLocationChanged(loc);
          }
             }
         }
    }
    
        // Initialize map options. For example:
        // mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    
    @Override
    public void onLocationChanged(Location location) {
    
       LatLng latlng=new LatLng(location.getLatitude(),location.getLongitude());// This methods gets the users current longitude and latitude.
    
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));//Moves the camera to users current longitude and latitude
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,(float) 14.6));//Animates camera and zooms to preferred state on the user's current location.
    }
    
        // TODO Auto-generated method stub
    

提交回复
热议问题