V
V
vPolyanchych2016-04-06 21:22:23
Android
vPolyanchych, 2016-04-06 21:22:23

google maps api. How to determine the user's city without asking him, through the navigator?

I need to know my location and not to put my coordinates in the code, but with the help of GPS.

public class MainActivity extends AppCompatActivity {
    private final LatLng LOCATION_BURNABY = new LatLng(49.27645, -122.917587);
    private final LatLng LOCATION_SURREY = new LatLng(49.187500, -122.849000);
    private LocationManager locationManager;
    private GoogleMap map;

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

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.addMarker(new MarkerOptions().position(LOCATION_SURREY));
    }

    public void City(View view) {
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        CameraUpdate update = CameraUpdateFactory.newLatLng(LOCATION_BURNABY);
        map.animateCamera(update);

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Yakushev, 2016-04-07
@vPolyanchych

There are two options for getting coordinates:
1. Get the last known coordinate - LocationManager.getLastKnownLocation(provider) or LocationServices.FusedLocationApi.getLastLocation . The second option will require connecting the gms library. This option is faster, but there is a big error. when this last coordinate was is not known.
2. Use the standard mechanism to get coordinates via LocationManager.requestLocationUpdates() . In this case, the coordinates can be obtained not only from GPS, but also from the cellular network. The option is a little longer in time, unlike the first, but gives a more accurate location.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question