【问题标题】:Getting GPS Location without google maps在没有谷歌地图的情况下获取 GPS 位置
【发布时间】:2018-08-05 13:41:44
【问题描述】:

我想在不使用谷歌地图的情况下在我的 android 项目中获取 gps 位置。 我开发了一个 在模拟器上运行 但无法在手机上获取位置的活动

【问题讨论】:

  • 你能展示一下你做了什么吗?
  • 向我们展示你的努力:)

标签: android


【解决方案1】:

此代码显示如何获取纬度和经度。希望对你有帮助


public class GPSTracker extends Service implements LocationListener {

private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
public boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled) {

        } else {
            this.canGetLocation = true;

            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

            }

            if(isGPSEnabled) {
                if(location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if(locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if(location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}


public void stopUsingGPS() {
    if(locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if(location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if(location != null) {
        longitude = location.getLongitude();
    }

    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

    alertDialog.setTitle("GPS is settings");

    alertDialog.setMessage("Turn on your GPS to find nearby helpers");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

@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

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

【讨论】:

    【解决方案2】:

    请下载此源代码,它将帮助您获得经纬度。 :) 从这两个你可以得到你的位置。希望这会有所帮助..

    乐于助人 :) https://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

    【讨论】:

      【解决方案3】:

      有两种方法可以获得当前位置 首先是使用GPS native api,其次是使用 goolge 地图库来做到这一点 第一种方法是

      // Acquire a reference to the system Location Manager
      LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
      
      // Define a listener that responds to location updates
      LocationListener locationListener = new LocationListener() {
          public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        makeUseOfNewLocation(location);
      }
      
      public void onStatusChanged(String provider, int status, Bundle extras) {}
      
      public void onProviderEnabled(String provider) {}
      
      public void onProviderDisabled(String provider) {}
        };
      
      // Register the listener with the Location Manager to receive location updates
             locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
      

      【讨论】:

        【解决方案4】:

        通过GPS经纬度简单获取当前位置:

        首先放入你的AndroidManifest.xml:
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

        然后向用户请求访问精细位置的权限:

        public void fineLocation(Activity activity) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
        
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
            if (requestCode == 1) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // get location because permission is granted.
                }
            }
        }
        

        然后得到当前的经纬度:

        @SuppressWarnings("ResourceType")
        public void gpsLoc(Context context) {
           android.location.LocationManager manager = (android.location.LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
           if (manager != null) {
              for (String provider : manager.getAllProviders()) {
                 Location location = manager.getLastKnownLocation(provider);
                 if (location != null) {
                    double lat = location.getLatitude();
                    double lon = location.getLongitude();
                 }
              }
           }
        }  
        

        【讨论】:

          【解决方案5】:

          试试这个

          @SuppressWarnings("ResourceType")
          public void gpsLoc(Context context) {
          android.location.LocationManager manager = (android.location.LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
          if (manager != null) {
            for (String provider : manager.getAllProviders()) {
               Location location = manager.getLastKnownLocation(provider);
               if (location != null) {
                  double lat = location.getLatitude();
                  double lon = location.getLongitude();
               }
             }
           }
          }  
          

          【讨论】:

            猜你喜欢
            • 2016-06-12
            • 1970-01-01
            • 1970-01-01
            • 2013-09-11
            • 2013-07-17
            • 2015-11-28
            • 1970-01-01
            • 1970-01-01
            • 2023-04-04
            相关资源
            最近更新 更多