简体   繁体   中英

moveCamera not updating to my current location on Google Maps Android?

I'm having a slight problem, when I run the app on my phone, my moveCamera is set on my previous location (not on my current one which I'm testing it on), and so is my addCircle (set around my previous location, not my current one).

Here is my code:

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

    }
}

I want the moveCamera to be on my current location, and my circle on my current location too. Any help please? Thanks

implement LocationListener. And on onLocationChanged redraw the circle

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM