2016-01-05 66 views
0
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
    LatLng bangalore = new LatLng(12.9667, 77.5667); 
    mMap.addMarker(new MarkerOptions().position(bangalore).title("Bengaluru")); 
    float zoomLevel = 12; 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bangalore, zoomLevel)); 
} 

代碼縮放到班加羅爾地區,但我需要爲我的當前位置我怎麼能直接放大到Android應用我的當前位置映射

+0

得到您的當前位置,並把變焦代碼,在該代碼你給LAT長班加羅爾這就是爲什麼它顯示在爲我工作班加羅爾 –

回答

0

使用此代碼location.getLatitude(),location.getLongitude( )用於獲取當前位置

   LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       Criteria criteria = new Criteria(); 
       Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 
       if (location != null) 
       { 
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(location.getLatitude(), location.getLongitude()), 13)); 

        CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude()))  // Sets the center of the map to location user 
        .zoom(17)     // Sets the zoom 
        .bearing(90)    // Sets the orientation of the camera to east 
        .tilt(40)     // Sets the tilt of the camera to 30 degrees 
        .build();     // Creates a CameraPosition from the builder 
       map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
       } 
+0

其位置謝謝 –

0

通過這個功能您當前的位置,你必須放置在您的標記。

private void moveToCurrentLocation(LatLng currentLocation) 
    { 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,15)); 
    // Zoom in, animating the camera. 
    mMap.animateCamera(CameraUpdateFactory.zoomIn()); 
// animating with a duration of 2 seconds. 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
    } 
0
LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

    mMap.getMap().setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 
      @Override 
      public void onMyLocationChange(Location location) { 
       if (location != null) { 
        LatLng latlng = new LatLng(location.getLatitude(), location.getLatitude()); 
        builder.include(latlng); 

        LatLngBounds bounds = builder.build(); 
        int padding = getScreenWidth(activity)/10; 
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
        mMap.getMap().animateCamera(cu); 
       } 

      } 
      }); 

public static int getScreenWidth(Activity activity) { 
     Display display = activity.getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     return size.x; 
    } 
相關問題