0

我的问题可能无关紧要,但我想知道我可以android.

4

2 回答 2

1

这是我在 android 中使用Location api 的方法。

第 1 步:使用提供的服务连接方法检查 Google Play 服务是否可用。

private boolean servicesConnected() {
    // Check that Google Play services is available
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getActivity());

    // If Google Play services is available
    if (ConnectionResult.SUCCESS == resultCode) {
        // In debug mode, log the status
        Log.d(LocationUtils.APPTAG,
                getString(R.string.play_services_available));

        // Continue
        return true;
        // Google Play services was not available for some reason
    } else {
        // Display an error dialog
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode,
                getActivity(), 0);
        if (dialog != null) {
            ErrorDialogFragment errorFragment = new ErrorDialogFragment();
            errorFragment.setDialog(dialog);
            errorFragment.show(myContext.getSupportFragmentManager(),
                    LocationUtils.APPTAG);
        }
        return false;
    }
}

第 2 步:确保您已经实现了 ShowError 对话框方法以及 ErrorDialog 片段

private void showErrorDialog(int errorCode) {

    // Get the error dialog from Google Play services
    Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode,
            getActivity(),
            LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST);

    // If Google Play services can provide an error dialog
    if (errorDialog != null) {

        // Create a new DialogFragment in which to show the error dialog
        ErrorDialogFragment errorFragment = new ErrorDialogFragment();

        // Set the dialog in the DialogFragment
        errorFragment.setDialog(errorDialog);

        // Show the error dialog in the DialogFragment
        errorFragment.show(myContext.getSupportFragmentManager(),
                LocationUtils.APPTAG);
    }
}

/**
 * Define a DialogFragment to display the error dialog generated in
 * showErrorDialog.
 */
@SuppressLint("ValidFragment")
public class ErrorDialogFragment extends DialogFragment {

    // Global field to contain the error dialog
    private Dialog mDialog;

    /**
     * Default constructor. Sets the dialog field to null
     */
    @SuppressLint("ValidFragment")
    public ErrorDialogFragment() {
        super();
        mDialog = null;
    }

    /**
     * Set the dialog to display
     * 
     * @param dialog
     *            An error dialog
     */
    public void setDialog(Dialog dialog) {
        mDialog = dialog;
    }

    /*
     * This method must return a Dialog to the DialogFragment.
     */
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return mDialog;
    }
} 

第 3 步:找到纬度和经度。

public void getAddress(View v) {
        // In Gingerbread and later, use Geocoder.isPresent() to see if a
        // geocoder is available.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD
                && !Geocoder.isPresent()) {
            // No geocoder is present. Issue an error message
            Toast.makeText(getActivity(), R.string.no_geocoder_available,
                    Toast.LENGTH_LONG).show();
            return;
        }

        if (servicesConnected()) {
            // Get the current location
            Location currentLocation = mLocationClient.getLastLocation();
            Lat = currentLocation.getLatitude();
            Lng = currentLocation.getLongitude();
            LatLng = Double.toString(Lat) + "," + Double.toString(Lng);
            // Show the location in a toast message
            Toast.makeText(getActivity(), LatLng, 0).show();
        } else {
            Toast.makeText(getActivity(), "servicesConnected() == false",
                    Toast.LENGTH_SHORT).show();
        }
    }

如果您需要更多信息,请参阅我在页面上的回答。

于 2014-07-08T05:47:54.927 回答
0

您不能在 android 中使用此 api,但您可以在 phonegap 技术中使用此 api,这是我们可以开发的跨平台混合应用程序

于 2014-07-08T05:15:20.313 回答