2016-07-10 14 views
1

Geocoderオブジェクトを使用してGoogleマップに住所を表示しようとしています。 これは私がFetchAddressIntentServiceクラスを作成してIntentServiceを拡張し、onHandleIntent()メソッドをオーバーライドし、明示的なインテントを使用してこのサービスを開始します。 しかし、私はstartService(インテント)onHandleIntent()メソッドを使用してサービスを開始するときに問題は、コールではありません。ジオコーダを使用して位置を表示する方法は?

私はandroid.developerサイトで提供されて同じコードは、次のとおりです。これは私のコードである

https://developer.android.com/training/location/display-address.html

を:

public class FetchAddressIntentService extends IntentService { 
    protected ResultReceiver mReceiver; 

    public FetchAddressIntentService() { 

     super("abc"); 

    } 


    @Override 
    protected void onHandleIntent(Intent intent) { 
     Toast.makeText(getBaseContext(),"onHandleIntent",Toast.LENGTH_LONG).show(); 
     String errorMessage=""; 

     Geocoder geocoder=new Geocoder(this, Locale.getDefault()); 

     Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA); 



     List<Address> addresses=null; 

     try { 
      addresses=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1); 
     } catch (IOException e) { 
      errorMessage="unhendle IO exception"; 
      Toast.makeText(getBaseContext(),"unhendle IO exception",Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 


     if(addresses==null && addresses.size()==0){ 
      if(errorMessage.isEmpty()){ 
       errorMessage="no address found"; 
       Toast.makeText(getBaseContext(),"no address found",Toast.LENGTH_LONG).show(); 
      } 

      deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage); 
     } 
     else{ 

      Address address=addresses.get(0); 
      ArrayList<String> arrayListFrag=new ArrayList<>(); 

      for (int i=0;i<address.getMaxAddressLineIndex();i++){ 

       arrayListFrag.add(address.getAddressLine(0)); 
      } 

      deliverResultToReceiver(Constants.SUCCESS_RESULT, 
        TextUtils.join(System.getProperty("line.separator"), 
          arrayListFrag)); 

     } 
    } 

    private void deliverResultToReceiver(int successResult, String message) { 

     Bundle bundle=new Bundle(); 
     bundle.putString(Constants.RESULT_DATA_KEY,message); 
     Toast.makeText(getBaseContext(),"resultData",Toast.LENGTH_LONG).show(); 
     mReceiver.send(successResult,bundle); 
    } 
} 

これらの方法は、開始の意図サービスのために使用されている。

protected void startIntentService(){ 
     Intent intent=new Intent(this,FetchAddressIntentService.class); 
     intent.putExtra(Constants.RECEIVER, mResultReceiver); 
     intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation); 
     startService(intent); 
     Toast.makeText(getBaseContext(),"startIntentService",Toast.LENGTH_LONG).show(); 
    } 


    public void fetchAddressButtonHandler(){ 
     if(mGoogleApiClient.isConnected() && mLastLocation!=null){ 
      startIntentService(); 
      Toast.makeText(getBaseContext(),"fetchAddressButtonHandler",Toast.LENGTH_LONG).show(); 
     } 


    } 

マニフェストファイル:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.missionandroid.googlemapproject"> 

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


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key" /> 

     <activity 
      android:name=".MapsActivity" 
      android:label="@string/title_activity_maps"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 


     </activity> 

<service android:name=".FetchAddressIntentService" 
    android:exported="false"></service> 
    </application> 

</manifest> 

答えて

2

このコードは、現在の緯度長いから完全なアドレスを取得するため正常に動作します:

はこれを試してみてください:

Geocoder geocoder; 
List<Address> addresses; 
geocoder = new Geocoder(this, Locale.getDefault()); 

addresses = geocoder.getFromLocation(your current lat, your current long, 1); 

String address = addresses.get(0).getAddressLine(0); 
String city = addresses.get(0).getLocality(); 
String state = addresses.get(0).getAdminArea(); 
String country = addresses.get(0).getCountryName(); 
String postalCode = addresses.get(0).getPostalCode(); 
String knownName = addresses.get(0).getFeatureName(); 

この情報がお役に立てば幸いです。ハッピーコーディング:)

+0

@xyz retyダックでも答えを受け入れてください – Pihu

関連する問題