繁体   English   中英

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

[英]How to get current location (Longitude and Latitude)

您好,我需要在开始片段时(在 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());

需要很多时间,因为您正在使用 GPS

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

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

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

为了使其更快,您可以尝试不使用 GPS: http : //developer.android.com/reference/android/location/LocationManager.html

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

你可以这样使用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM