facebook twitter hatena line email

「Android/緯度経度」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(緯度経度取得サンプル)
行38: 行38:
 
     @Override
 
     @Override
 
     public void onLocationChanged(Location location) {
 
     public void onLocationChanged(Location location) {
         Log.i("test", "緯度:" + location.getLatitude());
+
         Log.i("test", "緯度:" + location.getLatitude()); // 35.6947000
         Log.i("test", "経度:" + location.getLongitude());
+
         Log.i("test", "経度:" + location.getLongitude()); // 139.6969000
 
     }
 
     }
 
     @Override
 
     @Override

2016年11月4日 (金) 14:52時点における版

緯度経度取得サンプル

AndroidManifest.xml

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

KzLocation.java

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class KzLocation implements LocationListener {
   public KzLocation(Context context) {
       LocationManager mLocationManager =
               (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
       final boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
       if (!gpsEnabled) {
           // GPSが有効でない or wifi
       }
       String pkgname = context.getApplicationContext().getPackageName();
       PackageManager pm = context.getPackageManager();
       if (pm.checkPermission("android.permission.ACCESS_FINE_LOCATION", pkgname) == PackageManager.PERMISSION_GRANTED // ) {
           || pm.checkPermission("android.permission.ACCESS_COARSE_LOCATION", pkgname) == PackageManager.PERMISSION_GRANTED
               ) {
           mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                   0, // 通知のための最小時間間隔
                   0, // 通知のための最小距離間隔
                   (LocationListener)this); // 位置情報リスナー
           Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
           if (location != null) {
               onLocationChanged(location);
           }
           mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 36000, 100, (LocationListener)this);
       }
   }
   @Override
   public void onLocationChanged(Location location) {
       Log.i("test", "緯度:" + location.getLatitude()); // 35.6947000
       Log.i("test", "経度:" + location.getLongitude()); // 139.6969000
   }
   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {
   }
   @Override
   public void onProviderEnabled(String provider) {
   }
   @Override
   public void onProviderDisabled(String provider) {
   }
}