2014-06-13 108 views
0

我正在嘗試創建我的地圖,以便打開到用戶的當前位置放大。但是,我在執行此操作時遇到問題。我見過幾個使用getLastKnownLocation()的人,但是這總是會爲我返回null,而我總是得到NullPointerException。我的代碼是用地圖放大當前位置打開的應用程序

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.CameraPosition; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.widget.Toast; 

public class WeatherMapActivity extends Activity implements LocationListener { 

private GoogleMap map; 
private Location location; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.weather_maps_main); 

    initializeMap(); 
    Criteria criteria = new Criteria(); 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    String provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 
    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    LatLng coordinate = new LatLng(lat, lng); 
    map.moveCamera(CameraUpdateFactory.newLatLng(coordinate)); 
} 

private void initializeMap() { 
    // check if map is created 
    if(map == null) { 

     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // creates the map 
     map.setMyLocationEnabled(true); // enables gps to track my location 
     map.getUiSettings().setMyLocationButtonEnabled(true); // enables the "return to my location" button on map 
     map.getUiSettings().setCompassEnabled(true); // enables the compass button on map 

     // check if map is created successfully or not 
     if (map == null) { 
      Toast.makeText(getApplicationContext(), 
        "Map could not be created", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    initializeMap(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    map.clear(); 
    MarkerOptions marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())); 
    marker.title("Current location"); 
    map.addMarker(marker); 
} 

@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 
} 

我試圖把在onLocationChanged(Location location)map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16));但放大到我的當前位置,每隔一段時間,當我試圖看地圖的其他部分。

那麼如何設置它,以便當我打開應用程序時,它會打開到當前位置放大並且不會縮放任何其他時間?

+0

getLastKnownLocarion返回null,直到GPS或網絡提供一個位置。你會知道什麼時候發生這種情況,因爲onLocationChanged的回調運行。 – NickT

+0

當我啓動地圖後,我該如何獲取當前位置?我看到顯示我的位置的藍點,但我不知道如何放大該位置。 – user2127726

+0

你不這樣做,你一直等到你從GPS或網絡獲得修復。 – NickT

回答

1

您可以使用CameraPosition來執行此操作。

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       Criteria criteria = new Criteria(); 

       Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 
       if (location != null) 
       { 
        //here where the camera animate and zoom to particular location. 
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(location.getLatitude(), location.getLongitude()), 14)); 

        CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude()))  // Sets the center of the map to location user 
        .zoom(18)     // 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)); 

       } 
相關問題