GPS 位置始终显示为 0.0、0.0(已实现 LocationListener)

Posted

技术标签:

【中文标题】GPS 位置始终显示为 0.0、0.0(已实现 LocationListener)【英文标题】:GPS location always displayed as 0.0, 0.0 (LocationListener implemented) 【发布时间】:2016-10-20 18:58:03 【问题描述】:

我一直在构建一个将当前 GPS 位置发送到存储号码的应用程序。但是,位置一直是 0.0, 0.0。

代码如下:

public class ContactsList extends AppCompatActivity 

Button addContacts;
Button emergencyButton;

ListView listView;

ArrayList<HashMap<String, String>> contactsList;
ArrayList<Contact> contactobjectsList;
ArrayList<String> phonenumbersList;

DatabaseHelper databaseHelper = new DatabaseHelper(ContactsList.this);

LocationManager locationManager;
LocationListener locationListener;

double latitudeVal, longitudeVal;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    addContacts = (Button) findViewById(R.id.moveButton);
    listView = (ListView) findViewById(R.id.contactslistView);
    emergencyButton = (Button) findViewById(R.id.emergency_button);

    contactobjectsList = new ArrayList<Contact>();
    contactobjectsList = databaseHelper.getContacts();

    phonenumbersList = new ArrayList<String>();
    phonenumbersList = databaseHelper.getNumbers();

    contactsList = new ArrayList<HashMap<String, String>>();
    contactsList = databaseHelper.getValues();

    // For GPS:

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() 
        @Override
        public void onLocationChanged(Location location) 
            if (location != null)                    

                latitudeVal = location.getLatitude();
                longitudeVal = location.getLongitude();
            
        

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) 

        

        @Override
        public void onProviderEnabled(String provider) 
            Toast toast = Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_LONG);
            toast.show();


        

        @Override
        public void onProviderDisabled(String provider) 

            Toast toast = Toast.makeText(getApplicationContext(), "Enable GPS", Toast.LENGTH_LONG);
            toast.show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);


        
    ;


    ListAdapter listAdapter = new SimpleAdapter(ContactsList.this, contactsList, R.layout.customlayout,
            new String[]DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_PERSONNAME, DatabaseHelper.COLUMN_PHONENUMBER,
            new int[]R.id._idcust, R.id.personnamecust, R.id.phonenumbercust);

    listView.setAdapter(listAdapter);


    addContacts.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            Intent move = new Intent(ContactsList.this, AddContact.class);
            startActivity(move);


        
    );


    emergencyButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);



            String printLat = Double.toString(latitudeVal);
            String printLon = Double.toString(longitudeVal);

            Toast toast = Toast.makeText(getApplicationContext(),printLat+", "+printLon,Toast.LENGTH_LONG);
            toast.show();

           // sendSMS(phonenumbersList);


        
    );




由于某种原因,LocationListener 类中指示的纬度和经度的默认值是返回的值 (0.0, 0.0)。我真的不知道我的代码有什么问题,在这一点上我非常感谢一些帮助。

【问题讨论】:

【参考方案1】:
<uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
    android:maxSdkVersion="22" />

从清单中删除 android:maxSdkVersion="22",解决问题,或者如果您的 targetSdkVersion > 21,那么您必须实现 ACCESS_FINE_LOCATION 的运行时权限。 p>

【讨论】:

【参考方案2】:

你应该添加这一行

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);

就在这一行之后

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

而不是在emergencyButton.setOnClickListener中

【讨论】:

这可能会帮助你***.com/questions/17519198/…

以上是关于GPS 位置始终显示为 0.0、0.0(已实现 LocationListener)的主要内容,如果未能解决你的问题,请参考以下文章

获取 GPS 坐标后发送消息

安装应用程序上的 Android getLocation 为 0.0

我正在尝试将 GPS 功能添加到我的 android 应用程序中,gps 坐标每次都会出现 0.0、0.0

请教一下GPS导航电文代码的含义

使用 LocationManager 时位置显示为空

Asmack 文件传输进度始终为 0.0