2016-08-04 28 views
1

最近Android開発を開始しました。アプリに経度と緯度の値を表示したいと考えています。しかし、getLastKnowLocation()は常にnullを返すので、requestLocationUpdate()を使用しようとしましたが、エラーがあります。ここに私のコードです:ここではメソッド 'requestLocationUpdatesを解決できません

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    double longitude = location.getLongitude(); 
    double latitude = location.getLatitude(); 


public final LocationListener locationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     if(location!=null){ 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 

      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,10,locationListener); 
    } 

}; 

は私の輸入です:

import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import com.google.android.gms.location.LocationListener; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.text.DateFormat; 
import java.util.Date; 
import java.util.Locale; 

そして、私の用途許可:

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

更新されたコード:

public final LocationListener locationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

}; 

答えて

1

あなたがしています間違ったを使用してLocationListener。インポートする必要がありますandroid.location.LocationListener

+0

こんにちは、ありがとうございます。 'LocationListener'の 'onStatusChanged(String、int、Bundle)'の抽象メソッドを実装するか実装する必要があります。 – whzeng

+0

よく確信していますが、新しいLocationListener() 2つのLocationListenerは完全に別のクラスであり、提供する必要のあるメソッドが異なります。 – ianhanniballake

+0

私の更新されたコードを見てください。私はこれを正しくしていますか? 'this'が_第1引数の型が間違っているというエラーがあります。 – whzeng

関連する問題