简体   繁体   中英

get current location on start

I need to display users current locations using gps. At startup it displays some random location in sea and when i'll click to gps icon it displays my current location (white circle with optic icon) 在此处输入图片说明 . How can I do to display that on startup? I'm new at android/java so feel free to correct me anything.

my code is:

public class MainMapsActivity extends FragmentActivity implements android.location.LocationListener {

    GoogleMap googleMap;


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


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


        googleMap = fm.getMap();


        googleMap.setMyLocationEnabled(true);



        LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);


        Criteria criteria = new Criteria();


        String provider = locationManager.getBestProvider(criteria, true);


        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
            onLocationChanged(location);
        }



        locationManager.requestLocationUpdates(provider, 20000, 0, this);
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    }


    @Override
    public void onLocationChanged(Location location) {

        TextView tvLocation = (TextView) findViewById(R.id.tv_location);


        double latitude = location.getLatitude();


        double longitude = location.getLongitude();


        LatLng latLng = new LatLng(latitude, longitude);


        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));


        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

    }
}

Create a data member of Location class.

  public static Location mLocation;

use OnMyLocationChangeListener on Map

   mMap.setOnMyLocationChangeListener(new   GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            mLocation = location;
                displayCurrentLocation();
        }
    });

      public  void displayCurrentLocation(){
    CameraPosition cameraPosition = new CameraPosition.Builder().
            target(new LatLng(mLocation.getLatitude(), mLocation.getLongitude())).
            tilt(60).
            zoom(15).
            bearing(0).
            build();
       mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

hope it will works.

You can achieve this in two ways

  • Run a service (background operation) and get current latitude and longitude at the app starting itself , before coming to the targeted screen
  • Expose methods in service which you can access from Activity
  • Show users current location from the service method input
  • Or else implement a listener which will every time update the users current location

You can refer the following Example 1 and Example 2

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