纬度和长度始终为零 [英] LATITUDE AND LONGITUDE ALWAYS COME as zero
问题描述
下面的代码总是返回零....作为经度和纬度...
我使用GPSTrack类获取当前位置,然后显示该地点到地图..但GPS追踪器总是返回ZERO ....
以下是我的代码>>>
MAP ACTIVITY.JAVA
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
gps =新GPSTracker(MapsActivity.this);
//检查GPS是否启用
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng sydney =新LatLng(经度,纬度);
Toast.makeText(MapsActivity.this,LAT>>+ latitude,Toast.LENGTH_SHORT).show();
Toast.makeText(MapsActivity.this,LONG>>+ longitude,Toast.LENGTH_SHORT).show();
mMap.addMarker(new MarkerOptions()。position(sydney).title(当前位置));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
else
{
gps.showSettingsAlert();
}
}
GPS跟踪器类
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// GPS状态标志
boolean isGPSEnabled = false;
//网络状态标志
boolean isNetworkEnabled = false;
// GPS状态标志
boolean canGetLocation = false;
位置位置; //位置
双纬度; //纬度
双经度; // longitude
//改变的最小距离以米为单位的更新
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10米
//以毫秒为单位的最小更新时间间隔
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟
//声明位置管理器
受保护的LocationManager locationManager;
公共GPSTracker(上下文环境){
this.mContext = context;
getLocation();
public Location getLocation(){
try {
locationManager =(LocationManager)mContext
.getSystemService(LOCATION_SERVICE);
//获取GPS状态
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
//获取网络状态
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled&&!isNetworkEnabled){
//没有启用网络提供程序
} else {
this.canGetLocation = true;
//首先从网络供应商获取位置
if(isNetworkEnabled){
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
// TODO:考虑调用
// ActivityCompat#requestPermissions
//在这里请求丢失权限,然后重写
// public void onRequestPermissionsResult(int requestCode,String [] permissions,
// int [] grantResults)
//处理用户授予允许。有关更多详细信息,请参阅文档
//获取ActivityCompat#requestPermissions。
// return;
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(网络,网络);
if(locationManager!= null){
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();
$ b //如果GPS启用使用GPS服务获得经纬度
if(isGPSEnabled){
if(location == null ){
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(已启用GPS,已启用GPS);
if(locationManager!= null){
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();
} catch(Exception e){
e.printStackTrace();
}
返回地点;
}
/ **
*停止使用GPS监听器
*调用此函数将停止在您的应用中使用GPS
* /
public void stopUsingGPS(){
if(locationManager!= null){
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&&& ActivityCompat.checkSelfPermission (this,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
}
locationManager.removeUpdates(GPSTracker.this);
$ b $ **
函数获得纬度
* /
public double getLatitude(){
if(location!= null){
latitude = location.getLatitude();
}
//返回纬度
返回纬度;
$ b / **
*获取经度的函数
* /
public double getLongitude(){
if(location!= null){
longitude = location.getLongitude();
}
//返回经度
返回经度;
}
/ **
*检查GPS / wifi的功能
*
* @return boolean
* /
public boolean canGetLocation(){
return this.canGetLocation;
}
/ **
*显示设置提示对话框的功能
*按Settings按钮将启动设置选项
* /
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
//设置对话框标题
alertDialog.setTitle(GPS is settings);
//设置对话框消息
alertDialog.setMessage(GPS未启用,是否要进入设置菜单?);
//按设置按钮
alertDialog.setPositiveButton(Settings,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int which){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
//按下取消按钮
alertDialog.setNegativeButton(取消,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int其中){
dialog.cancel();
}
});
//显示提示信息
alertDialog.show();
}
@Override
public void onLocationChanged(Location location){
}
@Override
public void onProviderDisabled(字符串提供者){
}
@Override
public void onProviderEnabled(String provider){
}
@Override
public void onStatusChanged(String provider,int status,Bundle extras){
}
@Override
public IBinder onBind(Intent arg0){
return null;
$ / code $ / pre
这是怎么回事...
解决方案您的代码—这是一个可怕的垃圾邮件,它一直被复制 - —只有在 getLastKnownLocation()
调用之一返回一个位置时才会返回一个位置。 getLastKnownLocation()
很可能会返回 null
,如果最近没有其他人请求位置的话。 b
$ b
无法总是按需获取位置。您的代码必须能够处理:
-
稍后可以使用某个位置,例如通过 onLocationChanged()
或
-
一个位置永远不可用,因为设备目前无法提供位置
使用 getLastKnownLocation()
作为优化,其中 onLocationChanged()
是查找位置的主要方法。查看这个示例应用作为示例。
The Below Code always Returns Zero....as Latitude and Longitude...
I am Using a GPSTrack Class To Get the current location of and then Show that Location into the Maps..but GPS Tracker Always Return ZERO....
Below is My Code >>>
MAP ACTIVITY.JAVA
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
gps = new GPSTracker(MapsActivity.this);
// check if GPS enabled
if(gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng sydney = new LatLng(latitude, longitude);
Toast.makeText(MapsActivity.this, "LAT >> " + latitude, Toast.LENGTH_SHORT).show();
Toast.makeText(MapsActivity.this, "LONG >> " + longitude, Toast.LENGTH_SHORT).show();
mMap.addMarker(new MarkerOptions().position(sydney).title("Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
else
{
gps.showSettingsAlert();
}
}
GPS Tracker Class
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return;
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
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;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/
public void stopUsingGPS() {
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
*/
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
*/
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will launch Settings Options
*/
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
what's Wrong with This...
解决方案 Your code — which is an awful piece of junk that keeps getting copied around — will only ever return a location if one of the getLastKnownLocation()
calls returns a location. getLastKnownLocation()
is very likely to return null
, if nothing else has been requesting locations recently.
There is no way to always get a location on demand. Your code has to be able to deal with:
A location becoming available later, such as via onLocationChanged()
, or
A location never being available, because the device is incapable of providing a location at the present time
Use getLastKnownLocation()
as an optimization, where onLocationChanged()
is your primary way of finding the location. See this sample app for an example.
这篇关于纬度和长度始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!