Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.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
自动显示位置google android_Android_Google Maps_Reverse Geocoding - Fatal编程技术网

自动显示位置google android

自动显示位置google android,android,google-maps,reverse-geocoding,Android,Google Maps,Reverse Geocoding,我正在努力解决自动显示反向地理编码结果的问题。当我点击按钮获取位置时,它可以正常工作,但我希望它只是在应用程序加载时自动显示位置 我的代码在一个名为geocoding的Java类中,最终我想在我已经创建的地图上的一个标记上显示这段代码 但是这个线程是为了消除按钮,并在地图加载时显示位置 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); se

我正在努力解决自动显示反向地理编码结果的问题。当我点击按钮获取位置时,它可以正常工作,但我希望它只是在应用程序加载时自动显示位置

我的代码在一个名为geocoding的Java类中,最终我想在我已经创建的地图上的一个标记上显示这段代码

但是这个线程是为了消除按钮,并在地图加载时显示位置

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // uses layout from mainactivity

    mResultReceiver = new AddressResultReceiver(new Handler());

    mLocationAddressTextView = (TextView) findViewById(R.id.location_address_view);
    mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
    mFetchAddressButton = (Button) findViewById(R.id.fetch_address_button);

    // Set defaults, then update using values stored in the Bundle.
    mAddressRequested = false;
    mAddressOutput = "";
    updateValuesFromBundle(savedInstanceState);

    updateUIWidgets();
    buildGoogleApiClient();
}
public void buttonOnClick(View view) { // this links the maps activity via the XML layout file
    startActivity(new Intent(Geocode.this, MapsActivity.class));
}

/**
 * Updates fields based on data stored in the bundle.
 */
private void updateValuesFromBundle(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        // Check savedInstanceState to see if the address was previously requested.
        if (savedInstanceState.keySet().contains(ADDRESS_REQUESTED_KEY)) {
            mAddressRequested = savedInstanceState.getBoolean(ADDRESS_REQUESTED_KEY);
        }
        // Check savedInstanceState to see if the location address string was previously found
        // and stored in the Bundle. If it was found, display the address string in the UI.
        if (savedInstanceState.keySet().contains(LOCATION_ADDRESS_KEY)) {
            mAddressOutput = savedInstanceState.getString(LOCATION_ADDRESS_KEY);
            displayAddressOutput();
        }
    }
}

/**
 * Builds a GoogleApiClient. Uses {@code #addApi} to request the LocationServices API.
 */
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}


public void fetchAddressButtonHandler(View view) {
    // We only start the service to fetch the address if GoogleApiClient is connected.
    if (mGoogleApiClient.isConnected() && mLastLocation != null) {
        startIntentService();
    }

    mAddressRequested = true;
    updateUIWidgets();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

/**
 * Runs when a GoogleApiClient object successfully connects.
 */
@Override
public void onConnected(Bundle connectionHint) {
    // Gets the best and most recent location currently available, which may be null
    // in rare cases when a location is not available.
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling

        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        // Determine whether a Geocoder is available.
        if (!Geocoder.isPresent()) {
            Toast.makeText(this, R.string.no_geocoder_available, Toast.LENGTH_LONG).show();
            return;
        }
        if (mAddressRequested) {
            startIntentService();
        }
    }
}

/**
 * Creates an intent, adds location data to it as an extra, and starts the intent service for
 * fetching an address.
 */
protected void startIntentService() {
    // Create an intent for passing to the intent service responsible for fetching the address.
    Intent intent = new Intent(this, FetchAddressIntentService.class);

    // Pass the result receiver as an extra to the service.
    intent.putExtra(Constants.RECEIVER, mResultReceiver);

    // Pass the location data as an extra to the service.
    intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation);


    startService(intent);
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
    // onConnectionFailed.
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}


@Override
public void onConnectionSuspended(int cause) {
    // The connection to Google Play services was lost for some reason. We call connect() to
    // attempt to re-establish the connection.
    Log.i(TAG, "Connection suspended");
    mGoogleApiClient.connect();
}

/**
 * Updates the address in the UI.
 */
protected void displayAddressOutput() {
    mLocationAddressTextView.setText(mAddressOutput);
}

/**
 * Toggles the visibility of the progress bar. Enables or disables the Fetch Address button.
 */
private void updateUIWidgets() {
    if (mAddressRequested) {
        mProgressBar.setVisibility(ProgressBar.VISIBLE);
        mFetchAddressButton.setEnabled(false);
    } else {
        mProgressBar.setVisibility(ProgressBar.GONE);
        mFetchAddressButton.setEnabled(true);
    }
}

/**
 * Shows a toast with the given text.
 */
protected void showToast(String text) {
    Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save whether the address has been requested.
    savedInstanceState.putBoolean(ADDRESS_REQUESTED_KEY, mAddressRequested);

    // Save the address string.
    savedInstanceState.putString(LOCATION_ADDRESS_KEY, mAddressOutput);
    super.onSaveInstanceState(savedInstanceState);
}

/**
 * Receiver for data sent from FetchAddressIntentService.
 */
class AddressResultReceiver extends ResultReceiver {
    public AddressResultReceiver(Handler handler) {
        super(handler);
    }

    /**
     *  Receives data sent from FetchAddressIntentService and updates the UI in MainActivity.
     */
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {

        // Display the address string or an error message sent from the intent service.
        mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
        displayAddressOutput();

        // Show a toast message if an address was found.
        if (resultCode == Constants.SUCCESS_RESULT) {
            showToast(getString(R.string.address_found));
        }

        // Reset. Enable the Fetch Address button and stop showing the progress bar.
        mAddressRequested = false;
        updateUIWidgets();
    }
}

}

将请求的madesrequest从False设置为True, 因此,在OnConnected中,可以调用startinentservice

// Set defaults, then update using values stored in the Bundle.
    mAddressRequested = true;
    mAddressOutput = "";
    updateValuesFromBundle(savedInstanceState);

将请求的MadessRequested从False设置为True, 因此,在OnConnected中,可以调用startinentservice

// Set defaults, then update using values stored in the Bundle.
    mAddressRequested = true;
    mAddressOutput = "";
    updateValuesFromBundle(savedInstanceState);

据我所知,你可以得到纬度和经度?然后你想在地图上显示这个纬度,并在地图活动中的标记上注明地址?据我所知,你可以得到纬度和经度?然后你想在地图上显示这个纬度,并在地图活动中的标记上注明地址活动负载?这应该是一个评论对不起,我不能发表评论,因为我还没有50个声誉,但我的回答应该是正确的。好吧,不管怎样,我试着编辑它,我想这应该是一个评论对不起,我不能发表评论,因为我还没有50个声誉,但我的回答应该是正确的。不管怎样,我试着编辑它。