【问题标题】:Android GPS Location of the User用户的 Android GPS 位置
【发布时间】:2013-01-08 16:05:14
【问题描述】:

我需要为我的应用获取用户位置,以便我可以根据两个纬度和经度在 Google 地图上显示路线。

如果用户 GPS 已关闭,它应该询问用户是否要打开 GPS,并且应该能够让他打开设置。

我正在尝试以下操作,但它直接将我带到设置我想知道如何让用户询问他是否想被带走。

是否有任何库可以有效地执行此操作,我更喜欢使用它来获取用户的纬度和经度。

【问题讨论】:

    标签: android gps locationmanager


    【解决方案1】:

    如果您要询问如何询问用户是否有兴趣被导航到设置页面,以便打开位置服务 - 我建议您简单地显示一个对话框。 这是我项目中的一个示例:

    // Presents dialog screen - location services
    private void askLocationDialog(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    
        alertDialogBuilder.setTitle(R.string.snoox_use_location_services_dialog);
    
        // set dialog message
        alertDialogBuilder.setMessage(R.string.would_you_like_to_turn_your_location_services_on_)
        .setCancelable(false)
        .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // opens the setting android screen
                Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(settingsIntent);
            }
        })
        .setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });
    
        // create alert dialog
        alertDialogBuilder.create().show();
    }
    

    如果您对完整示例感兴趣,我发现这篇文章很有帮助: How do I find out if the GPS of an Android device is enabled

    【讨论】:

      【解决方案2】:

      您需要做几件事。

      首先,要合并 Google 地图,您可以在 here 获得完整的参考资料。

      简单的步骤:

      1 按照步骤here:这将有助于在您的屏幕上添加一个简单的谷歌地图。

      2 为了能够获取您自己的位置,您需要在 android 中使用 LocationListener 和 LocationManager。为此,首先在您的活动中实现 LocationListener。

      public class LocationActivity extends Activity implements LocationListener

      3 然后你需要在你的 onCreate() 方法中实例化一些设置

           @Override
            public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
      
              // Get the location manager
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          // Define the criteria how to select the provider
          Criteria criteria = new Criteria();
          criteria.setAccuracy(Criteria.ACCURACY_FINE);
          provider = locationManager.getBestProvider(criteria, false);
          Location location = locationManager.getLastKnownLocation(provider);
      
          // Initialize the location fields
          if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
          } 
        }
      

      4 您需要能够请求定期更新位置信息。将此包含在您的 onResume() 方法中。

      @Override
        protected void onResume() {
          super.onResume();
          locationManager.requestLocationUpdates(provider, 400, 1, this);
        }
      

      5 如果应用进入暂停周期,则不需要进行这些更新。

      @Override
        protected void onPause() {
          super.onPause();
          locationManager.removeUpdates(this);
        }
      

      6 您在第 2 步中的位置监听器实现要求您有一个 onLocationChanged 监听器,实现它:

      @Override   
      public void onLocationChanged(Location location) {
      int lat = (int) (location.getLatitude());
      int lng = (int) (location.getLongitude());
      }
      

      7 添加这两种方法以通知您的位置设置提供商 - GPS 或网络。

      public void onProviderDisabled(String arg0) {
          Toast.makeText(this, "Disabled provider " + provider,
                      Toast.LENGTH_SHORT).show();
      }
      
      public void onProviderEnabled(String arg0) {
          Toast.makeText(this, "Enabled new provider " + provider,
                      Toast.LENGTH_SHORT).show();
      }
      

      8 现在我们需要将其链接到您的谷歌地图。我将向您展示一个使用 google maps API 生成市场以显示您当前位置的示例。其他用法可以从 API 中推断出来。

      首先在您的代码中创建私有字段:

      private GoogleMap mMap;
      Marker m;
      

      9 在您的 onCreate 方法中添加这些 - 这会将您的默认标记位置实例化为 0,0 纬度和经度。

      mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                      .getMap();
      m = mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0))
                      .title("Position"));
      

      10 在您的 onLocationChanged 方法中,我们需要在位置更改时刷新此标记。所以添加:

      m.setPosition(new LatLng(lat, lng));
      m.setTitle("Your Position");
      
      // Move the camera instantly to marker with a zoom
      // of 15.
                                  mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15));
      

      这将是一种使用您的位置更新标记的简单方法,并且应该是您在 Android 中的 Google 地图和位置 API 的一个很好的介绍。

      要检测 GPS 是否打开,您可以使用@Dror 提供的答案 :) 希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        相关资源
        最近更新 更多