简体   繁体   English

用户的Android GPS位置

[英]Android GPS Location of the User

I need to get the Users Location for my app so that i can display the directions on Google Maps based on two Lat and Long. 我需要获取我的应用的“用户位置”,以便可以在基于“纬度”和“纬度”两个位置的Google Maps上显示路线。

If the Users GPS is switched off it should ask the user if he wants to switch on the GPS and should be able to take him the Settings to switching it on. 如果关闭了用户GPS,则应询问用户是否要打开GPS,并应该能够接受设置以将其打开。

i am trying the following but it takes me directly to the settings i wonder how to let the user ask if he wants to be taken away. 我正在尝试以下操作,但它直接将我带到设置,我想知道如何让用户询问他是否要被带走。

Is there any Library that does this efficiently i would prefer using it to get the Lat and Long of the user. 是否有任何图书馆可以有效地做到这一点,我更喜欢使用它来获取用户的纬度和经度。

If you're asking how to ask the user if he's interested in being navigated to the setting page, in order to turn location services on - I would recommend simply presenting a Dialog. 如果您要询问如何询问用户是否对导航到设置页面感兴趣,为了打开位置服务,我建议您仅显示一个对话框。 Here's an example from my project : 这是我的项目中的一个示例:

// 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();
}

if you're interested in a full example I found this post helpful: How do I find out if the GPS of an Android device is enabled 如果您对一个完整的示例感兴趣,我认为这篇文章很有帮助: 如何确定是否启用了Android设备的GPS

You will need to do several things. 您将需要做几件事。

Firstly, to incorporate Google Maps, your complete reference is available here . 首先,要整合Google Maps,请在此处获得完整的参考。

In simple steps: 简单的步骤:

1 Follow the steps here : this will help add a simple google maps to your screen. 1按照以下步骤在这里 :这将有助于增加一个简单的谷歌地图到你的屏幕。

2 To be able to get your own location you will need to use the LocationListener and LocationManager in android. 2为了能够获取自己的位置,您将需要在android中使用LocationListener和LocationManager。 To do this, first implement the LocationListener in your activity. 为此,首先在您的活动中实现 LocationListener。

public class LocationActivity extends Activity implements LocationListener

3 Then you need to instantiate a few settings in your onCreate() method 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 You need to be able to request for regular location updates. 4您需要能够请求常规位置更新。 Include this in your onResume() method. 将此包含在onResume()方法中。

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

5 If the app falls into the pause cycle, these updates shouldn't need to come. 5如果应用程序进入暂停周期,则不需要进行这些更新。

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

6 Your location listener implementation from step 2 requires that you have an onLocationChanged listener, implement it: 6您从步骤2开始的位置侦听器实现要求您有一个onLocationChanged侦听器,并将其实现:

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

7 Add these two methods to be notified of the provider of your location setting - the GPS or the Network. 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 Now we need to link this up to your google maps. 8现在,我们需要将其链接到您的Google地图。 I will show you one example of using the google maps API to be able to generate a market to show your current location. 我将向您展示一个使用google maps API来生成市场以显示您当前位置的示例。 The other usages can be inferred from the API. 可以从API推断其他用法。

First create private fields in your code: 首先在您的代码中创建私有字段:

private GoogleMap mMap;
Marker m;

9 Add these in your onCreate method - this instantiates your default marker position as 0,0 latitude and longitude. 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 In your onLocationChanged method, we need to refresh this marker as location changes. 10在您的onLocationChanged方法中,我们需要在位置更改时刷新此标记。 So add: 因此添加:

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

This will be a simple way of updating the marker with your position, and should be a good intro to your Google Maps and location API in android. 这将是一种使用位置更新标记的简单方法,并且应该是android中的Google Maps和location API的良好介绍。

To detect if GPS is on or not you can use the answer provided by @Dror :) Hope it helps! 要检测GPS是否打开,可以使用@Dror提供的答案:)希望它能有所帮助!

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

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