【问题标题】:How to determine my current location (longitude,latitude and altitude)?如何确定我当前的位置(经度、纬度和高度)?
【发布时间】:2011-07-14 10:15:53
【问题描述】:

我想确定我当前的位置(纬度、经度和高度)。 在我的数据库中,我有一个按表位置(id、名称、地址、经度、纬度、高度)的表内容

如何确定我当前的位置和最近的位置?

我在 manifest.xml 中添加了这个:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

如何使用方法 (.getlongitude(); .getLatitude(); .getaltitude()

documentation 这不清楚。

提前谢谢你。

【问题讨论】:

    标签: android geolocation gps android-location


    【解决方案1】:

    您可以选择获取最后一个已知位置,或收听更新。最简单的方法是获取最新的:

    位置 lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    如果你想要一个准确的位置,你需要使用 requestLocationUpdates() 来监听更新。

    另外,记得检查GPS设备是否开启,见How do I find out if the GPS of an Android device is enabled

    要查找最近的地址,请参阅http://developer.android.com/reference/android/location/Geocoder.html

    【讨论】:

      【解决方案2】:

      首先启用你的 GPS。

          private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
          private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
      
          public static double lontitube,latitude;
      
          protected LocationManager locationManager;
          protected Location currentLocation;
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
              locationManager.requestLocationUpdates(
                      LocationManager.GPS_PROVIDER, 
                      MINIMUM_TIME_BETWEEN_UPDATES, 
                      MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                      new MyLocationListener()
              );
      }
      
       protected void performReverseGeocodingInBackground() {
              showCurrentLocation();
      
          }
      
          protected void showCurrentLocation() {
      
              currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      
              if (currentLocation != null) {
                  String message = String.format(
                          "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                          currentLocation.getLongitude(), currentLocation.getLatitude()
      
                  );
                  lontitube=currentLocation.getLongitude();
                  latitude=currentLocation.getLatitude();
      
                  System.out.println("----lati----longi----"+latitude+"\n"+lontitube);
      
                     Toast.makeText(Demo.this, message,
                     Toast.LENGTH_LONG).show();
              }
      
          }   
      
          private class MyLocationListener implements LocationListener {
      
              public void onLocationChanged(Location location) {
      
             String message = String.format(
                          "New Location \n Longitude: %1$s \n Latitude: %2$s",
                          location.getLongitude(), location.getLatitude()
                  );
                  Toast.makeText(Demo.this, message, Toast.LENGTH_LONG).show();
              }
      
              public void onStatusChanged(String s, int i, Bundle b) {
                  Toast.makeText(Demo.this, "Provider status changed",
                          Toast.LENGTH_LONG).show();
              }
      
              public void onProviderDisabled(String s) {
                  Toast.makeText(Demo.this,
                          "Provider disabled by the user. GPS turned off",
                          Toast.LENGTH_LONG).show();
              }
      
              public void onProviderEnabled(String s) {
                  Toast.makeText(Demo.this,
                          "Provider enabled by the user. GPS turned on",
                          Toast.LENGTH_LONG).show();
              }
      
          }
      

      在AndroidManifest中添加

      ACCESS_FINE_LOCATION

      GPS 使用许可。

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 2013-01-11
        相关资源
        最近更新 更多