2013-01-24 134 views
0

我需要获取我的应用的用户位置,以便我可以基于两个经纬度在Google地图上显示路线。用户的Android GPS位置

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

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

有没有任何图书馆这样做有效,我宁愿使用它来获得用户的经纬度。

回答

1

你需要做几件事情。

首先,要纳入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现在我们需要将其链接到您的谷歌地图。我会告诉你一个使用谷歌地图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)); 

这将是与你的位置更新标记的一个简单的方法,应该是一个很好的介绍到您的谷歌地图和位置API中的机器人。

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

2

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

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