Android - Location API: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „http://developer.android.com/training/location/index.html == Einleitung == === Location Client === Die Location Services senden die User Position durch einen lo…“)
 
 
Zeile 8: Zeile 8:
 
Location Services is part of the Google Play services APK. you should always check that the APK is installed before you attempt to connect to Location Services
 
Location Services is part of the Google Play services APK. you should always check that the APK is installed before you attempt to connect to Location Services
 
   GooglePlayServicesUtil.isGooglePlayServicesAvailable()
 
   GooglePlayServicesUtil.isGooglePlayServicesAvailable()
 +
 +
== Schnellstart ==
 +
=== Position abfragen ===
 +
* Create LocationClient (lc)
 +
* Verbinde lc mit LocationServices
 +
* Rufe die getLastLocation() Methode des lc auf
  
 
== GooglePlayService Test ==
 
== GooglePlayService Test ==

Aktuelle Version vom 15. Oktober 2013, 10:48 Uhr

http://developer.android.com/training/location/index.html


Einleitung[Bearbeiten]

Location Client[Bearbeiten]

Die Location Services senden die User Position durch einen location client. Dieser ist eine Instanz der Location Services Klasse LocationClient. Alle Location Infos gehen durch diesen Client

Google Play services APK[Bearbeiten]

Location Services is part of the Google Play services APK. you should always check that the APK is installed before you attempt to connect to Location Services

 GooglePlayServicesUtil.isGooglePlayServicesAvailable()

Schnellstart[Bearbeiten]

Position abfragen[Bearbeiten]

  • Create LocationClient (lc)
  • Verbinde lc mit LocationServices
  • Rufe die getLastLocation() Methode des lc auf

GooglePlayService Test[Bearbeiten]

public class MainActivity extends FragmentActivity {
    ...
    // Global constants
    /*
     * Define a request code to send to Google Play services
     * This code is returned in Activity.onActivityResult
     */
    private final static int
            CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    ...
    // Define a DialogFragment that displays the error dialog
    public static class ErrorDialogFragment extends DialogFragment {
        // Global field to contain the error dialog
        private Dialog mDialog;
        // Default constructor. Sets the dialog field to null
        public ErrorDialogFragment() {
            super();
            mDialog = null;
        }
        // Set the dialog to display
        public void setDialog(Dialog dialog) {
            mDialog = dialog;
        }
        // Return a Dialog to the DialogFragment.
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mDialog;
        }
    }
    ...
    /*
     * Handle results returned to the FragmentActivity
     * by Google Play services
     */
    @Override
    protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        // Decide what to do based on the original request code
        switch (requestCode) {
            ...
            case CONNECTION_FAILURE_RESOLUTION_REQUEST :
            /*
             * If the result code is Activity.RESULT_OK, try
             * to connect again
             */
                switch (resultCode) {
                    case Activity.RESULT_OK :
                    /*
                     * Try the request again
                     */
                    ...
                    break;
                }
            ...
        }
     }
    ...
    private boolean servicesConnected() {
        // Check that Google Play services is available
        int resultCode =
                GooglePlayServicesUtil.
                        isGooglePlayServicesAvailable(this);
        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            // In debug mode, log the status
            Log.d("Location Updates",
                    "Google Play services is available.");
            // Continue
            return true;
        // Google Play services was not available for some reason
        } else {
            // Get the error code
            int errorCode = connectionResult.getErrorCode();
            // Get the error dialog from Google Play services
            Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
                    errorCode,
                    this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);

            // If Google Play services can provide an error dialog
            if (errorDialog != null) {
                // Create a new DialogFragment for 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(getSupportFragmentManager(),
                        "Location Updates");
            }
        }
    }
    ...
}