moveCamera没有更新到Google Maps Android上的当前位置?

时间:2013-12-04 06:15:24

标签: android google-maps

我遇到了一个小问题,当我在手机上运行应用程序时,我的moveCamera设置在我之前的位置(不是我正在测试它的当前位置),所以我的addCircle(设置)我以前的位置,而不是我现在的位置)。

这是我的代码:

public class MapDraw extends FragmentActivity {

    static GoogleMap googleMap;
    LatLng myPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_g_maps_circle);

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
        if(location!=null){
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            LatLng latLng = new LatLng(latitude, longitude);
            myPosition = new LatLng(latitude, longitude);

            circleDraw(latitude,longitude);
            zoomIn(latitude,longitude);
        }
    }

    public void circleDraw(double i, double ii) {

        googleMap.addCircle(new CircleOptions()
        .center(new LatLng(i, ii))
        .radius(1000)
        .strokeColor(Color.BLACK)
        .strokeWidth(2)
        .fillColor(Color.argb(50, 238, 116, 116)));
    }

    public void zoomIn(double Lat, double Long) {

        CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(Lat,Long));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
        googleMap.moveCamera(center);
        googleMap.animateCamera(zoom);

    }
}

我希望moveCamera在我当前的位置,我的圈子也在我当前的位置。 有什么帮助吗?感谢

1 个答案:

答案 0 :(得分:2)

实现LocationListener。并在onLocationChanged上重绘圆圈

import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;

public class MapDraw extends FragmentActivity implements LocationListener {

    static GoogleMap googleMap;
    LocationManager locationManager;
    String provider;
    LatLng myPosition;

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

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            onLocationChanged(location);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    public void circleDraw(double i, double ii) {

        googleMap.addCircle(new CircleOptions().center(new LatLng(i, ii))
                .radius(1000).strokeColor(Color.BLACK).strokeWidth(2)
                .fillColor(Color.argb(50, 238, 116, 116)));
    }

    public void zoomIn(double Lat, double Long) {

        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(Lat,
                Long));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
        googleMap.moveCamera(center);
        googleMap.animateCamera(zoom);

    }

    @Override
    public void onLocationChanged(Location location) {
        googleMap.clear();// clean the map
        Toast.makeText(this, "Location Changed", Toast.LENGTH_SHORT).show();
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        myPosition = new LatLng(latitude, longitude);

        circleDraw(latitude, longitude);
        zoomIn(latitude, longitude);
    }

    @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

    }

}