简体   繁体   中英

How to get current location (Longitude and Latitude)

Hello i need to get current coordinates when i start fragment (in onResume()). I try next:

public class FragmentNear extends Fragment implements  LocationListener{
View v;
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView tvLess1;
String lat;
String provider;
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    v=inflater.inflate(R.layout.fragment_near, null);

    tvLess1=(TextView)v.findViewById(R.id.tv_less_1);
    return v;
}

@Override
public void onResume() {
    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


    super.onResume();
}


@Override
public void onLocationChanged(Location location) {
Log.d("myLogs", "onChangeLocation");
//tvLess1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
//Log.d("myLogs","Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}

@Override
public void onProviderDisabled(String provider) {
Log.d("myLogs","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("myLogs","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("myLogs","status");
}
 }

It get coordinates for me about one minute, why it take so much time? And then onChangeLocation is calling all time, but i want update coordinates only in when onResume is calling? How can i do it?


SOLUTION IS to use next:

LocationManager service = (LocationManager)     getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = service.getBestProvider(criteria, false);
    Location location = service.getLastKnownLocation(provider);
    LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());

It takes so much time because you are using GPS

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

You device must have a valid signal from the GPS satelittes and this will take time depending on your environmental situation. In buildings it will take more time and outside it will be much faster as there is nothing blocking the straight line between your device and the satelittes.

The method "onLocationChange" is always called if there is a change in the current GPS location - Which will happen very often, as you device is moving or more satelittes are locked so your current location is tracked more precisely.

For making it faster you could try not using GPS: http://developer.android.com/reference/android/location/LocationManager.html

Here is a list of possible other ways to get your current location - But this wont fix the continuously call of "onLocationChange" - This is normal behaviour. If you do not want to do anything in this method, just leave it empty.

You can use like this.

  @Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 4);
        gm.animateCamera(cameraUpdate);
       Marker myMarker = gm.addMarker(new MarkerOptions()
        .position(latLng)
        .title("You are here") 

        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

        locationManager.removeUpdates(this);
}

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