【问题标题】:how to make condition that if my GPS is not open如果我的 GPS 未打开,如何设置条件
【发布时间】:2014-09-25 01:34:48
【问题描述】:

如何设置如果我的 GPS 没有打开,我的应用会先打开 GPS 选项,然后在打开我的 GPS 后,它会继续搜索我的位置。请帮助并提前感谢您的答案。

这是我的 .java 文件

static final LatLng HAMBURG = new LatLng(53.558, 9.927);
 static final LatLng KIEL = new LatLng(53.551, 9.993);

GoogleMap googlemap;
@Override
protected void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
    if(status!=ConnectionResult.SUCCESS){
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();
    }
    else{
        SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googlemap = mf.getMap();
        googlemap.setMyLocationEnabled(true);
        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, this);

    }
}
@Override
public void onLocationChanged(Location location) {


    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map

    googlemap.animateCamera(CameraUpdateFactory.zoomTo(15));
    googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude))
    .title("Your Here!"));

       }


@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}
@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}









}

【问题讨论】:

    标签: java android eclipse gps google-maps-api-2


    【解决方案1】:

    我把它推到一个方法如下:

    /**
     * Sets up the devices location services to report location and updates the lat and long of the device if available
     */
    public void initialiseLocationServices() {
        // Initialise objects
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps_enabled = false;
        boolean network_enabled = false;
        boolean hasLocation;
    
        try {
            gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ignored) {
    
        }
    
        try {
            network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ignored) {
    
        }
    
        //  I am only interested in at least one being on.
        // hasLocation = gps_enabled || network_enabled;
        //if(hasLocation) { my way of doing things
    
        // You want to do it like this:
        if(!gps_enabled) {
            Intent gpsOptionsIntent = new Intent(  
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
            startActivity(gpsOptionsIntent);
        } else {
            SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            googlemap = mf.getMap();
            googlemap.setMyLocationEnabled(true);
    
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();
    
            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
    
            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);
    
            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 20000, 0, this);
        }
    

    注意:这只是伪代码,您需要处理 GPS 设置等的更改,我尚未测试此代码。

    【讨论】:

      【解决方案2】:

      您可以为此使用位置管理器

      private LocationManager locationManager;
      public boolean isGPSEnabled;
      
      locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      
      if(isGPSEnabled)
      {
          if(locationManager != null)
          {
              //TODO
          }
      }
      

      【讨论】:

      • 如果您在应用启动时尝试访问 GPS,那么您可以将其放入您的 onCreate() 方法中:)
      • 我将它插入到 protected void onCreate(Bundle savedInstanceState) { 为什么它在 private LocationManager locationManager 行中有错误;公共布尔isGPSEnabled; locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);它说非法修饰符。
      • 不要将私有LocationManager和公共布尔值放在onCreate()方法中,将它们声明为全局变量
      • 您能否为此发送另一个代码,包括我的代码和您的代码?请?提前致谢
      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多