[Android] Locationを取得する

LocationListenerインターフェイスを実装したリスナーを作成する getSystemServiceメソッドでLocationManagerを取得する LocationManager requestLocationUpdatesメソッドでリスナーをLocationManagerに登録する

でもこれ実機がないと動かないみたい。起動すらしない。

package com.mamezou.android.example.location;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LocationExampleActivity extends Activity {

  protected static final int MAX_GEOCODE_RESULT = 10;

  private Button button;
  private TextView status;
  private TextView latitude;
  private TextView longitude;

  private LocationListener gpsListener = new LocationListener() {

    // 位置情報を取得するたびに実行
    public void onLocationChanged(Location location) {
      // Location#getLatitude() で緯度を取得
      latitude.setText("緯度 = " + location.getLatitude());
      // Location#getLongitude() で経度を取得
      longitude.setText("経度 = " + location.getLongitude());
    }

    // 以下3つも実装する必要がある。
    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

  };

  private boolean started;

  private View.OnClickListener listener = new View.OnClickListener() {
    public void onClick(View view) {
      LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      if (started) {
        manager.removeUpdates(gpsListener);
        status.setText("停止中");
        latitude.setText("");
        longitude.setText("");
        started = false;
      } else {
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, gpsListener);
        status.setText("起動中");
        started = true;
      }
    };
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int wrap = ViewGroup.LayoutParams.WRAP_CONTENT;
    int fill = ViewGroup.LayoutParams.FILL_PARENT;
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    button = new Button(this);
    button.setWidth(100);
    button.setText("起動・停止");
    button.setOnClickListener(listener);
    layout.addView(button, new LinearLayout.LayoutParams(wrap, wrap));
    status = new TextView(this);
    status.setText("停止");
    layout.addView(status, new LinearLayout.LayoutParams(fill, wrap));
    latitude = new TextView(this);
    layout.addView(latitude, new LinearLayout.LayoutParams(fill, wrap));
    longitude = new TextView(this);
    layout.addView(longitude, new LinearLayout.LayoutParams(fill, wrap));
    setContentView(layout);
  }

  @Override
  protected void onResume() {

    super.onResume();
  }

  @Override
  protected void onPause() {
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    manager.removeUpdates(gpsListener);
    manager = null;
    super.onPause();
  }
}

マニフェストファイル

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mamezou.android.example.location"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LocationExampleActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
    <uses-library android:name="com.google.android.maps" />
    </application>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

</manifest>
Share