簡體   English   中英

無法讓android將Lat和Lng一致地輸出到Textview

[英]Can't get android to output Lat & Lng consistently to Textview

我已經做到了,所以當我在手機上重新安裝APK時,它確實會輸出Lat&Lng,但它會寫入5次,但之后卻無法正常工作。如何使它正常工作?

在日志中,它輸出我正在按按鈕,但似乎什么也沒做。 我正在測試的設備是運行Android 7的Galaxy S7 Edge

下面的代碼

    getLocation = (LocationManager) getSystemService(LOCATION_SERVICE);
    listenLocation = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            townName.append(location.getLatitude() + " " + location.getLongitude());



        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);

        }
    };
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        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) {
           requestPermissions(new String[]{
                   android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION,
                   android.Manifest.permission.INTERNET
           }, 10);
            return;
        }
    }else{
        configureButton();
    }



}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    switch(requestCode){
        case 10:
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {
    location.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            getLocation.requestLocationUpdates("gps", 5000, 0, listenLocation);
        }
    });
}

onLocationChanged()方法中使用belo代碼。

  @Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

      LatLng latLng = new LatLng(latitude, longitude);
      mMap.addMarker(new MarkerOptions().position(latLng).title("My Location"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
     mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
     latLngString = location.getLatitude() + "," + location.getLongitude();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM