facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
(同じ利用者による、間の1版が非表示)
行18: 行18:
 
  import android.util.Log;
 
  import android.util.Log;
 
  public class KzLocation implements LocationListener {
 
  public class KzLocation implements LocationListener {
 +
    @SuppressLint("MissingPermission")
 
     public KzLocation(Context context) {
 
     public KzLocation(Context context) {
 
         LocationManager mLocationManager =
 
         LocationManager mLocationManager =
行56: 行57:
 
     }
 
     }
 
  }
 
  }
 +
 +
呼び出し
 +
location = new KzLocation(getApplicationContext());
 +
Button btn1 = (Button) findViewById(R.id.button);
 +
btn1.setOnClickListener(new View.OnClickListener() {
 +
    public void onClick(View v) {
 +
        if (location.latitude.length() > 0 && location.longitude.length() > 0) {
 +
            Log.i("location lati", location.latitude); // 35.6947000
 +
          Log.i("location long", location.longitude); // 139.6969000
 +
      }
 +
    }
 +
});

2018年8月9日 (木) 16:07時点における最新版

注意

androidの設定/位置情報をonにしないと取得できない

メインスレッド以外のスレッド内では取得できない

緯度経度取得サンプル

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 {
   @SuppressLint("MissingPermission")
   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) {
   }
}

呼び出し

location = new KzLocation(getApplicationContext());
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       if (location.latitude.length() > 0 && location.longitude.length() > 0) {
           Log.i("location lati", location.latitude); // 35.6947000
         Log.i("location long", location.longitude); // 139.6969000
      }
    }
});