如何获取当前位置(经度和纬度)

问题描述 投票:1回答:2

你好我需要在我开始片段时获取当前坐标(在onResume()中)。我接下来尝试:

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

它给我的坐标约一分钟,为什么需要这么多时间?然后onChangeLocation一直在调用,但我只想在onResume调用时才更新坐标?我该怎么做?


解决方案是使用下一个:

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());
android google-maps android-location
2个回答
0
投票

由于您使用GPS,因此需要很长时间

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

您的设备必须具有来自GPS卫星的有效信号,这将需要时间,具体取决于您的环境情况。在建筑物中,它将花费更多的时间,而在外面它会更快,因为没有任何东西阻挡你的设备和卫星之间的直线。

如果当前GPS位置发生变化,则始终会调用“onLocationChange”方法 - 这种情况会经常发生,因为设备正在移动或更多的satelittes被锁定,因此您可以更精确地跟踪当前位置。

为了加快速度,你可以尝试不使用GPS:http://developer.android.com/reference/android/location/LocationManager.html

以下列出了获取当前位置的其他可能方法 - 但这不会修复“onLocationChange”的持续调用 - 这是正常行为。如果您不想在此方法中执行任何操作,请将其留空。


0
投票

你可以像这样使用。

  @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);
}
© www.soinside.com 2019 - 2024. All rights reserved.