【问题标题】:How to get current location (Longitude and Latitude)如何获取当前位置(经度和纬度)
【发布时间】:2013-08-20 09:34:33
【问题描述】:

您好,当我开始片段时(在 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


【解决方案1】:

因为您使用的是 GPS,所以需要花费很多时间

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

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

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

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

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

【讨论】:

  • 谢谢,但您能提供其他获取当前位置的方法吗?
  • 我没有可以显示的代码,这将是简短易懂的,但您可以查看 MyLocationOverlay - 我假设您正在使用 GoogleMaps,因为您的标签说 - 然后您可以随时请求当前位置。看看developers.google.com/maps/documentation/android/v1/reference/…这是我在我的应用程序中的做法,效果很好,但正如我已经说过的,我无法向你展示简短易懂的代码
  • 但我使用的是谷歌地图 v2,我没有使用 mapView。 MyLocatonOVerlay(context ,Mapview)
【解决方案2】:

你可以这样使用。

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

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多