1

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

Adz
  • 2,809
  • 10
  • 43
  • 61
  • Its moving to last location because you are getting the Last known location in your code only. – GrIsHu Dec 04 '13 at 06:30
  • So how do I make it get the current location? – Adz Dec 04 '13 at 06:40
  • check out http://stackoverflow.com/questions/18425141/android-google-maps-api-v2-zoom-to-current-location it might help you. – GrIsHu Dec 04 '13 at 06:44

1 Answers1

2

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

    }

}
Md. Monsur Hossain Tonmoy
  • 11,045
  • 2
  • 22
  • 19
  • Hey Monsur, I have done the code exactly like yours, but the same problem is happening. It zooms in my last location (not my current one), if I zoom hit the "My location" GUI that Google has then it goes to my location. Also the circle is still drawn back to where I previously was. Maybe there's something wrong with my phone? – Adz Dec 04 '13 at 11:31
  • At first the map will zoom to your last location because it takes sometime to get the location. But when it will find your location it will update the location and redraw the circle. each time you change the location it will clear the map and redraw the circle. It perfectly works on my mobile galaxy s3 mini. Please check your internet connection and also you may turn on the GPS for more accuracy. – Md. Monsur Hossain Tonmoy Dec 04 '13 at 13:15
  • Hi, thanks for the help. I had to turn my phone on and off each time I run my app. Only then will it update the zoom onto your current location. – Adz Dec 04 '13 at 22:59