Skip to main content

位置情報を取得して画面に表示してみた

GPSまたはネットワークWi-Fiから現在位置の情報を取得して、テキストで表示する。

LocationSwitcherActivity.java ソース

package bz.hi_five.locationswitcher;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
/**
 * 位置情報取得して色々やってみよう処理
 *
 */
public class LocationSwitcherActivity extends Activity implements
  LocationListener {
 protected TextView locationView;
 protected TextView statusView;
 protected TextView providerView;
 protected LocationManager mLocationManager;
 protected String bestProvider;

 // アクティビティ生成時に実行する
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  findViews();
  // 位置情報の初期設定
  initLocationService();
 }

 // ユーザの操作受付時に呼ばれる
 @Override
 protected void onResume() {
  super.onResume();
        // 最小で○○msec周期、
  // 最小で○○mの位置変化の場合
  // どんなに変化しても5000msecのより短い間隔では通知されず、1mより小さい変化の場合は通知されない
  mLocationManager.requestLocationUpdates(bestProvider, 5000, 1, this);
 }

 // ユーザの操作中断時に実行する
 @Override
 protected void onPause() {
  super.onPause();
  mLocationManager.removeUpdates(this);
 }

 // 位置情報の初期設定
 protected void initLocationService() {
  mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  Criteria criteria = new Criteria();

// この設定でGPS(ACCURACY_FINE)とネットワーク(ACCURACY_COARSE)を切り替え出来ます
criteria.setAccuracy(Criteria.ACCURACY_FINE);
  criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
  bestProvider = mLocationManager.getBestProvider(criteria, true);
  providerView.setText(bestProvider);
 }

 // 画面表示の初期設定
 protected void findViews() {
  locationView = (TextView) findViewById(R.id.location_view);
  statusView  = (TextView) findViewById(R.id.status_view);
  providerView = (TextView) findViewById(R.id.provider_view);
 }

 // 緯度と経度を表示する
 protected void refreshGeoLocation(Location location) {
  String desc = "経度:" + location.getLongitude() + ",  緯度:" + location.getLatitude();
  locationView.setText(desc);
 }

 // 位置情報が変更された場合に実行する
 @Override
 public void onLocationChanged(Location location) {
  refreshGeoLocation(location);
  statusView.setText("Log: onLocationChanged");
 }

 @Override
 public void onProviderDisabled(String provider) {
  statusView.setText("Log: onProviderDisabled");
 }

 @Override
 public void onProviderEnabled(String provider) {
  statusView.setText("Log: onProviderEnabled");
 }

 // 位置情報のステータスが変更された場合に実行する(onLocationChangedの後)
 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
  statusView.setText("Log: onStatusChanged");
 }
}


main.xmlのソース

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Location Switcher"
   />
<TextView
   android:id="@+id/status_view"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=""
   />
<TextView
   android:id="@+id/provider_view"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=""
   />
<TextView
   android:id="@+id/location_view"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=""
   />
</LinearLayout>


AndroidManifest.xmlに以下を記入します

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />





Comments

Popular posts from this blog

遂に来た11速用スギノのキャパシティブースター!装着方法・性能検証

なんと!多くの方が待望のスギノエンジニアリングのビッグプーリー「CAPACITY BOOSTER」の11速用が発売です!装着方法や性能を紹介します! https://youtu.be/zyJIgUXi-x0?si=zIaV_LM0hNB3AlqB #ロードバイク #ビッグプーリー

知りたい表示機能ほぼ全紹介!COOSPO CS500 サイクルコンピューター

いつも多く質問されるので調べます!購入する方が知りたい表示機能をほぼ全部と、開封から走行テストまでを紹介しちゃいます。 https:// youtu.be/lLfY8H0U12A?si =-V5KFh2Blg1yWyXk … #ロードバイク #サイクルコンピューター

実走検証!キャパシティブースター・ゼロ・フリクション動作解説

スギノエンジニアリングからこの春発売予定の新型キャパシティブースター、ゼロ・フリクションを走行インプレッションと検証してみました!どのように動いているかも解説します!チェーンを外さない対策にもなりそう 😀 https://youtu.be/EOXcJdQPkQs?si=vjFOv43CnyVhBL0f #ロードバイク #ビッグプーリー