打开应用程序与地图放大在当前位置



我正在尝试创建我的地图,以便它打开用户的当前位置放大。然而,我有麻烦这样做。我见过一些人使用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
}
}

我试过把map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16));放在onLocationChanged(Location location)中,但是当我试图查看地图的其他部分时,这经常会放大到我当前的位置。

那么我如何设置它,这样当我打开应用程序时,它会放大到当前位置,而不会在其他时间放大?

您可以使用camerposition来实现此功能。

      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));
                }

最新更新