2016-09-20 3 views
0

私はアプリを開発していますが、これはユーザーの現在地を表示するためにGoogleマップを使用しています。経度と緯度をテキスト形式に変換して住所を表示するにはどうすればよいですか?

次のコードは使用していますが、住所の結果には地図の現在の位置のみが表示され、緯度と経度が表示されていません。現在の経度と緯度から現在の住所をテキストフィールドに表示しますか?

//java 

public class LocationActivity extends Activity { 

    private TextView locationText; 
    private TextView addressText, textview; 
    private GoogleMap map; 
    String mob_no; 
    private boolean loggedIn = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_location); 

     locationText = (TextView) findViewById(R.id.location); 
     addressText = (TextView) findViewById(R.id.address); 
    // textview=(TextView)findViewById(R.id.textView_euser); 

     SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false); 
     mob_no = sharedPreferences.getString(Config.PHONE_SHARED_PREF, "Not Available"); 
    // textview.setText(String.valueOf(mob_no)); 

     //replace GOOGLE MAP fragment in this Activity 
     replaceMapFragment(); 
    } 

    private void replaceMapFragment() { 

      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 


     // Enable Zoom 
     map.getUiSettings().setZoomGesturesEnabled(true); 

     //set Map TYPE 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     //enable Current location Button 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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; 
     } 
     map.setMyLocationEnabled(true); 

     //set "listener" for changing my location 
     map.setOnMyLocationChangeListener(myLocationChangeListener()); 
    } 

    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() { 
     return new GoogleMap.OnMyLocationChangeListener() { 
      @Override 
      public void onMyLocationChange(Location location) { 
       LatLng loc = new LatLng(location.getLatitude(), location.getLongitude()); 
       double longitude = location.getLongitude(); 
       double latitude = location.getLatitude(); 

       Marker marker; 
       marker = map.addMarker(new MarkerOptions().position(loc)); 
       map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f)); 
       locationText.setText("You are at [" + longitude + " ; " + latitude + " ]"); 

       //get current address by invoke an AsyncTask object 
        new GetAddressTask(LocationActivity.this).execute(String.valueOf(latitude), String.valueOf(longitude)); 
       // getCompleteAddressString(longitude,latitude); 
      } 
     }; 
    } 

    public void callBackDataFromAsyncTask(String address) { 
     addressText.setText(address); 
    } 

/* @SuppressLint("LongLogTag") 
    private String getCompleteAddressString(double LATITUDE, double LONGITUDE) { 
     String strAdd = ""; 
     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 
      if (addresses != null) { 
       Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder("Address:"); 

       for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
       } 
       strAdd = strReturnedAddress.toString(); 
       addressText.setText(strAdd); 
       Log.w("My Current loction address", "" + strReturnedAddress.toString()); 
      } else { 
       Log.w("My Current loction address", "No Address returned!"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.w("My Current loction address", "Canont get Address!"); 
     } 
     return strAdd; 
    } */ 

} 



//getaddress 

public class GetAddressTask extends AsyncTask<String, Void, String> { 

    private LocationActivity activity; 

    public GetAddressTask(LocationActivity activity) { 
     super(); 
     this.activity = activity; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     Geocoder geocoder; 
     List<Address> addresses; 
     geocoder = new Geocoder(activity, Locale.getDefault()); 

     try { 
      addresses = geocoder.getFromLocation(Double.parseDouble(params[0]), Double.parseDouble(params[1]), 1); 

      //get current Street name 
      String address = addresses.get(0).getAddressLine(0); 

      //get current province/City 
      String province = addresses.get(0).getAdminArea(); 

      //get country 
      String country = addresses.get(0).getCountryName(); 

      //get postal code 
      String postalCode = addresses.get(0).getPostalCode(); 

      //get place Name 
      String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL 

      return "Street: " + address + "\n" + "City/Province: " + province + "\nCountry: " + country 
        + "\nPostal CODE: " + postalCode + "\n" + "Place Name: " + knownName; 

     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return "IOE EXCEPTION"; 

     } catch (IllegalArgumentException ex) { 
      ex.printStackTrace(); 
      return "IllegalArgument Exception"; 
     } 

    } 

    /** 
    * When the task finishes, onPostExecute() call back data to Activity UI and displays the address. 
    * @param address 
    */ 
    @Override 
    protected void onPostExecute(String address) { 
     // Call back Data and Display the current address in the UI 
     activity.callBackDataFromAsyncTask(address); 
    } 
} 
+2

可能な複製を助けます(http:// stackoverflowの[緯度と経度から完全なアドレスを取得するには?]。 com/questions/9409195/how-to-get-complete-address-from-latitude-and-longitude) – W4R10CK

答えて

0

あなたはStringdoubleを変換するDouble.toString()を使用することができます。また、あなたは+を使用することができます:あなたは、このような表示する小数点以下の桁数として、出力をより細かく制御する必要

"" + latitude 

場合は、String.format()を使用することができます。

0
public static void getAddressFromLocation(final double latitude, final double longitude, 
               final Context context, final Handler handler) { 
     Thread thread = new Thread() { 
      @Override 
      public void run() { 
       Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
       String result = null; 
       try { 
        List<Address> addressList = geocoder.getFromLocation(
          latitude, longitude, 1); 
        if (addressList != null && addressList.size() > 0) { 
         Address address = addressList.get(0); 
         StringBuilder sb = new StringBuilder(); 
         for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
          sb.append(address.getAddressLine(i)).append("\n"); 
         } 
         sb.append(address.getLocality()).append("\n"); 
         sb.append(address.getPostalCode()).append("\n"); 
         sb.append(address.getCountryName()); 
         result = sb.toString(); 
        } 
       } catch (IOException e) { 
        Log.e(TAG, "Unable connect to Geocoder", e); 
       } finally { 
        Message message = Message.obtain(); 
        message.setTarget(handler); 
        if (result != null) { 
         message.what = 1; 
         Bundle bundle = new Bundle(); 
         result = "Latitude: " + latitude + " Longitude: " + longitude + 
           "\n\nAddress:\n" + result; 
         bundle.putString("address", result); 
         message.setData(bundle); 
        } else { 
         message.what = 1; 
         Bundle bundle = new Bundle(); 
         result = "Latitude: " + latitude + " Longitude: " + longitude + 
           "\n Unable to get address for this lat-long."; 
         bundle.putString("address", result); 
         message.setData(bundle); 
        } 
        message.sendToTarget(); 
       } 
      } 
     }; 
     thread.start(); 
    } 

詳細は下のリンクをチェックしてくださいは: - http://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/

そのがあなたにの

関連する問題