2014-06-13 112 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)); 

       } 
相关问题