用户的Android GPS位置

时间:2013-01-24 19:26:39

标签: android gps locationmanager

我需要获取我的应用的用户位置,以便我可以在两个Lat和Long上显示Google地图上的路线。

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

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

是否有任何图书馆可以有效地执行此操作,我更愿意使用它来获取用户的Lat和Long。

2 个答案:

答案 0 :(得分:2)

如果你问如何询问用户他是否有兴趣导航到设置页面,为了打开位置服务 - 我建议只提出一个Dialog。 这是我项目的一个例子:

// 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

答案 1 :(得分:1)

你需要做几件事。

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

简单的步骤:

1按照here步骤操作:这有助于在屏幕上添加简单的Google地图。

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提供的答案:)希望它有所帮助!