棉花糖GPS坐标onChangeLocation没有调用

时间:2017-03-26 20:02:10

标签: android gps

我只需要我的程序的纬度,经度和海拔高度,这是我的代码,但似乎onLocationChanged永远不会被调用。

MainActivity.Java

    public class MainActivity extends AppCompatActivity implements LocationListener {
        protected LocationManager locationManager;
        protected LocationListener locationListener;
        protected Context context;
        double MyLong=0;
        double MyLati=0;

     @Override
     protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtLat = (TextView) findViewById(R.id.textview1);

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

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // coarse permission is granted
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                0, 0, this);

    }
    else { // permission is not granted, request for permission
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) { // show some info to user why you want this permission
            Toast.makeText(this, "Allow Location Permission to use this functionality.", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/);

        }
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            0, 0, this);
}


@Override
onLocationChanged(Location location){
    MyLong = location.getLongitude(); // this is Y in my map
    MyLati = location.getLatitude();  // this is X in my map

    txtLat = (TextView) findViewById(R.id.textview1);
    String msg = "Coordinates are: long:" + MyLong + " lati: "+MyLati;
    txtLat.setText(" "+msg+" ");

AndroidManifest

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.staykov.sunlight">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

如何让onChangeLocation工作?这段代码有什么问题?我确实获得了我的初始字符串“Hello world”,但它没有更改为包含经度和纬度的字符串。如果我不得不猜测我在onCreate上做错了什么,但我不知道是什么。

另一件事是“受保护的LocationListener locationListener;”从未使用过。

0 个答案:

没有答案