Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 融合位置提供程序客户端在反向地理编码中返回null_Android_Reverse Geocoding_Fusedlocationproviderclient - Fatal编程技术网

Android 融合位置提供程序客户端在反向地理编码中返回null

Android 融合位置提供程序客户端在反向地理编码中返回null,android,reverse-geocoding,fusedlocationproviderclient,Android,Reverse Geocoding,Fusedlocationproviderclient,我正在开发一个应用程序,用户可以从中获取当前的位置地址。代码对我来说似乎很好,但当我运行应用程序时,它会在单个活动中被击中,并且只显示进度圈。getlastlocation()返回null。我正在使用融合位置提供程序客户端 地址提取服务类 public class FetchAddressIntentService extends IntentService { private static final String TAG = "FetchAddressIS"; /**

我正在开发一个应用程序,用户可以从中获取当前的位置地址。代码对我来说似乎很好,但当我运行应用程序时,它会在单个
活动中被击中,并且只显示
进度圈
。getlastlocation()返回null。我正在使用融合位置提供程序客户端

地址提取服务类

public class FetchAddressIntentService extends IntentService {
    private static final String TAG = "FetchAddressIS";

    /**
     * The receiver where results are forwarded from this service.
     */
    private ResultReceiver mReceiver;

    /**
     * This constructor is required, and calls the super IntentService(String)
     * constructor with the name for a worker thread.
     */
    public FetchAddressIntentService() {
        // Use the TAG to name the worker thread.
        super(TAG);
    }

    /**
     * Tries to get the location address using a Geocoder. If successful, sends an address to a
     * result receiver. If unsuccessful, sends an error message instead.
     * Note: We define a {@link ResultReceiver} in * MainActivity to process content
     * sent from this service.
     *
     * This service calls this method from the default worker thread with the intent that started
     * the service. When this method returns, the service automatically stops.
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        String errorMessage = "";

        mReceiver = intent.getParcelableExtra(Constants.RECEIVER);

        // Check if receiver was properly registered.
        if (mReceiver == null) {
            Log.wtf(TAG, "No receiver received. There is nowhere to send the results.");
            return;
        }

        // Get the location passed to this service through an extra.
        Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);

        // Make sure that the location data was really sent over through an extra. If it wasn't,
        // send an error error message and return.
        if (location == null) {
            errorMessage = "no location data provided";
            Log.wtf(TAG, errorMessage);
            deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
            return;
        }

        // Errors could still arise from using the Geocoder (for example, if there is no
        // connectivity, or if the Geocoder is given illegal location data). Or, the Geocoder may
        // simply not have an address for a location. In all these cases, we communicate with the
        // receiver using a resultCode indicating failure. If an address is found, we use a
        // resultCode indicating success.

        // The Geocoder used in this sample. The Geocoder's responses are localized for the given
        // Locale, which represents a specific geographical or linguistic region. Locales are used
        // to alter the presentation of information such as numbers or dates to suit the conventions
        // in the region they describe.
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());

        // Address found using the Geocoder.
        List<Address> addresses = null;

        try {
            // Using getFromLocation() returns an array of Addresses for the area immediately
            // surroungetString(R.string.invalid_lat_long_used)ding the given latitude and longitude. The results are a best guess and are
            // not guaranteed to be accurate.
            addresses = geocoder.getFromLocation(
                    location.getLatitude(),
                    location.getLongitude(),
                    // In this sample, we get just a single address.
                    1);
        } catch (IOException ioException) {
            // Catch network or other I/O problems.
            errorMessage = "service not available";
            Log.e(TAG, errorMessage, ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            // Catch invalid latitude or longitude values.
            errorMessage ="invalid lat and  long" ;
            Log.e(TAG, errorMessage + ". " +
                    "Latitude = " + location.getLatitude() +
                    ", Longitude = " + location.getLongitude(), illegalArgumentException);
        }

        // Handle case where no address was found.
        if (addresses == null || addresses.size()  == 0) {
            if (errorMessage.isEmpty()) {
                errorMessage = "no address found";
                Log.e(TAG, errorMessage);
            }
            deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
        } else {
            Address address = addresses.get(0);
            ArrayList<String> addressFragments = new ArrayList<>();

            // Fetch the address lines using {@code getAddressLine},
            // join them, and send them to the thread. The {@link android.location.address}
            // class provides other options for fetching address details that you may prefer
            // to use. Here are some examples:
            // getLocality() ("Mountain View", for example)
            // getAdminArea() ("CA", for example)
            // getPostalCode() ("94043", for example)
            // getCountryCode() ("US", for example)
            // getCountryName() ("United States", for example)
            for(int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
                addressFragments.add(address.getSubLocality());
            }
            Log.i(TAG, "address found");
            deliverResultToReceiver(Constants.SUCCESS_RESULT,
                    TextUtils.join(System.getProperty("line.separator"), addressFragments));
        }
    }

    /**
     * Sends a resultCode and message to the receiver.
     */
    private void deliverResultToReceiver(int resultCode, String message) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.RESULT_DATA_KEY, message);
        mReceiver.send(resultCode, bundle);
    } 
}
这些是获取地址的活动

这是日志

07-19 15:14:32.504 1219-1313/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
07-19 15:14:32.507 5369-5414/com.example.user.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-19 15:14:32.567 5369-5414/com.example.user.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
07-19 15:14:32.567 5369-5414/com.example.user.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-19 15:14:32.568 2713-2772/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation()
07-19 15:14:32.613 5369-5414/com.example.user.myapplication D/EGL_emulation: eglCreateContext: 0x7f47995a1d20: maj 2 min 0 rcv 2
07-19 15:14:32.623 5369-5414/com.example.user.myapplication D/EGL_emulation: eglMakeCurrent: 0x7f47995a1d20: ver 2 0 (tinfo 0x7f478db47080)
07-19 15:14:32.746 2738-5125/com.google.android.gms I/Icing: Indexing 72BFAEF03056CDF63B46406CB190BC9702C5FE10 from com.google.android.googlequicksearchbox
07-19 15:14:32.754 5369-5414/com.example.user.myapplication D/EGL_emulation: eglMakeCurrent: 0x7f47995a1d20: ver 2 0 (tinfo 0x7f478db47080)
07-19 15:14:32.830 2738-5125/com.google.android.gms I/Icing: Indexing done 72BFAEF03056CDF63B46406CB190BC9702C5FE10
07-19 15:14:32.833 2738-5125/com.google.android.gms I/Icing: Indexing 81B472D5B60E335901521374F7E4943B2B43137C from com.google.android.gms
07-19 15:14:32.851 5369-5369/com.example.user.myapplication W/MainActivity: onSuccess:null
07-19 15:14:32.945 1909-1942/system_process I/ActivityManager: Displayed com.example.user.myapplication/.Main2Activity: +1s951ms
07-19 15:14:32.963 2738-5125/com.google.android.gms I/Icing: Indexing done 81B472D5B60E335901521374F7E4943B2B43137C
07-19 15:14:40.907 1909-1935/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
07-19 15:15:02.424 1219-1323/? D/hwcomposer: hw_composer sent 1619 syncs in 60s
07-19 15:15:56.618 2738-5417/com.google.android.gms I/EventLogChimeraService: Aggregate from 1531991756065 (log), 1531991756065 (data)
07-19 15:15:57.135 1909-3285/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
07-19 15:16:02.418 1219-1323/? D/hwcomposer: hw_composer sent 3092 syncs in 60s

在关键位置添加一些日志语句,或者使用调试器详细了解正在发生的事情。我这样做了,getLastLocation()返回null可能与我看到的相同,它使用google api客户端(不推荐用于获取地址),我使用fused location provider客户端
public class LocationShow extends AppCompatActivity {

    TextView t1;
    String output,output2;
    private static final String ADDRESS_REQUESTED_KEY = "address-request-pending";
    private static final String LOCATION_ADDRESS_KEY = "location-address";
    String mAddressOutput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t1=findViewById(R.id.place2);

        output = getIntent().getStringExtra("addressOutput");

        showAddress(output);
    }

    private void showAddress(String place) {

        t1.setText(place);
    }
}
07-19 15:14:32.504 1219-1313/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
07-19 15:14:32.507 5369-5414/com.example.user.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-19 15:14:32.567 5369-5414/com.example.user.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
07-19 15:14:32.567 5369-5414/com.example.user.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-19 15:14:32.568 2713-2772/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation()
07-19 15:14:32.613 5369-5414/com.example.user.myapplication D/EGL_emulation: eglCreateContext: 0x7f47995a1d20: maj 2 min 0 rcv 2
07-19 15:14:32.623 5369-5414/com.example.user.myapplication D/EGL_emulation: eglMakeCurrent: 0x7f47995a1d20: ver 2 0 (tinfo 0x7f478db47080)
07-19 15:14:32.746 2738-5125/com.google.android.gms I/Icing: Indexing 72BFAEF03056CDF63B46406CB190BC9702C5FE10 from com.google.android.googlequicksearchbox
07-19 15:14:32.754 5369-5414/com.example.user.myapplication D/EGL_emulation: eglMakeCurrent: 0x7f47995a1d20: ver 2 0 (tinfo 0x7f478db47080)
07-19 15:14:32.830 2738-5125/com.google.android.gms I/Icing: Indexing done 72BFAEF03056CDF63B46406CB190BC9702C5FE10
07-19 15:14:32.833 2738-5125/com.google.android.gms I/Icing: Indexing 81B472D5B60E335901521374F7E4943B2B43137C from com.google.android.gms
07-19 15:14:32.851 5369-5369/com.example.user.myapplication W/MainActivity: onSuccess:null
07-19 15:14:32.945 1909-1942/system_process I/ActivityManager: Displayed com.example.user.myapplication/.Main2Activity: +1s951ms
07-19 15:14:32.963 2738-5125/com.google.android.gms I/Icing: Indexing done 81B472D5B60E335901521374F7E4943B2B43137C
07-19 15:14:40.907 1909-1935/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
07-19 15:15:02.424 1219-1323/? D/hwcomposer: hw_composer sent 1619 syncs in 60s
07-19 15:15:56.618 2738-5417/com.google.android.gms I/EventLogChimeraService: Aggregate from 1531991756065 (log), 1531991756065 (data)
07-19 15:15:57.135 1909-3285/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
07-19 15:16:02.418 1219-1323/? D/hwcomposer: hw_composer sent 3092 syncs in 60s