当我查看地图的其他部分时,Google地图会指向我当前的位置

时间:2014-05-11 05:24:47

标签: android google-maps-android-api-2

这是TrackMap.java

public class TrackMap extends FragmentActivity implements LocationListener {

    GoogleMap map;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track_map);

        boolean lowPowerMoreImportantThanAccurancy = true;
        LocationRequest request = LocationRequest.create();
        request.setPriority(lowPowerMoreImportantThanAccurancy ? 
                LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY: 
                LocationRequest.PRIORITY_HIGH_ACCURACY);

        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

        // Enabling MyLocation Layer of Google Map
        map.setMyLocationEnabled(true);

        Marker marker = map.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0)).draggable(true)
                // Set Opacity specified as a float between 0.0 and 1.0,
                // where 0 is fully transparent and 1 is fully opaque.
                .alpha(0.7f).flat(true)
                .getPosition());

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        map.clear();

        MarkerOptions mp = new MarkerOptions();

        mp.position(new LatLng(location.getLatitude(), location.getLongitude()));

        mp.title("my position");

        map.addMarker(mp);

        map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(location.getLatitude(), location.getLongitude()), 16));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

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

    }



}

我想知道为什么在查看地图的其他部分时,它会指向我当前的位置。非常感谢您的帮助。我真的不知道删除特定代码以满足我的需求

1 个答案:

答案 0 :(得分:1)

因为每次调用onLocationChanged时,您都会将相机设置为传入的新位置。