2017-05-23 1 views
0

Ich folgte einem Beispiel von Android Geocoding, um Längengrad für eine Adresse Lernprogramm zu erhalten, es funktionierte, aber ich möchte die Längen- und Breitengradkoordinaten getrennt abrufen, konnte aber nicht.Ich möchte Longitude und Latitude getrennt von einer bestimmten Adresse abrufen

Hier ist mein Code:

public class MainActivity extends AppCompatActivity { 
    Button addressButton; 
    TextView addressTV; 
    TextView latLongTV; 
    static TextView txtLatitude; 
    TextView txtLongitude; 


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


     addressTV = (TextView) findViewById(R.id.addressTV); 
     latLongTV = (TextView) findViewById(R.id.latLongTV); 
    txtLatitude = (TextView) findViewById(R.id.txtLatitude); 
     txtLatitude= (TextView) findViewById(R.id.txtLongitude); 

     addressButton = (Button) findViewById(R.id.addressButton); 
     addressButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       EditText editText = (EditText) findViewById(R.id.addressET); 
       String address = editText.getText().toString(); 

       GeocodingLocation locationAddress = new GeocodingLocation(); 
       locationAddress.getAddressFromLocation(address, 
         getApplicationContext(), new GeocoderHandler()); 
      } 
     }); 

    } 

    private class GeocoderHandler extends Handler { 
     @Override 
     public void handleMessage(Message message) { 
      String locationAddress,latitude ,longitude ; 

      switch (message.what) { 
       case 1: 
        Bundle bundle = message.getData(); 
        locationAddress = bundle.getString("address"); 
        latitude = bundle.getString("latitude"); 
        longitude = bundle.getString("longitude"); 
        break; 
       default: 
        locationAddress = null; 
        latitude = null; 
        longitude= null; 
      } 
      latLongTV.setText(locationAddress); 
      txtLatitude.setText(latitude); 
//  txtLongitude.setText(longitude); 
     } 
    } 

    private static class GeocodingLocation { 


      private static final String TAG = "GeocodingLocation"; 

      public void getAddressFromLocation(final String locationAddress, 
               final Context context, final Handler handler) { 
       Thread thread = new Thread() { 
        @Override 
        public void run() { 
         Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
         String result = null; 
         String strLatitude = null, strLongitude = null; 
         try { 
          List<Address> addressList = geocoder.getFromLocationName(locationAddress, 1); 
          if (addressList != null && addressList.size() > 0) { 
           Address address = addressList.get(0); 
           double latitude = address.getLatitude(); 
           double longitude = address.getLongitude(); 
           StringBuilder sb = new StringBuilder(); 
           sb.append(address.getLatitude()).append("\n"); 
           sb.append(address.getLongitude()).append("\n"); 
           result = sb.toString(); 
           strLatitude = String.valueOf(latitude); 
           strLongitude= String.valueOf(longitude); 
          } 
         } catch (IOException e) { 
          Log.e(TAG, "Unable to connect to Geocoder", e); 
         } finally { 
          Message message = Message.obtain(); 
          message.setTarget(handler); 
          if (result != null) { 
           message.what = 1; 
           Bundle bundle = new Bundle(); 
           result = "Address: " + locationAddress + 
             "\n\nLatitude and Longitude :\n" + result; 
           bundle.putString("address", result); 
           bundle.putString("Latitude", strLatitude); 
           bundle.putString("Longitude", strLongitude); 
           message.setData(bundle); 
          } else { 
           message.what = 1; 
           Bundle bundle = new Bundle(); 
           result = "Address: " + locationAddress + 
             "\n Unable to get Latitude and Longitude for this address location."; 
           bundle.putString("address", result); 
           message.setData(bundle); 
          } 
          message.sendToTarget(); 
         } 
        } 
       }; 
       thread.start(); 
      } 
     } 
+1

Wie genau funktioniert es nicht? Kracht es, und wenn ja, wo? Erhalten Sie ungültige Werte, Nullwerte oder überhaupt keine Werte? Bitte geben Sie uns einen Hinweis, wo Sie suchen müssen. –

Antwort

2

Nach this answer, Bündel Tasten sind case-sensitive. Sie setzen die Saiten mit:

bundle.putString("Latitude", strLatitude); 
bundle.putString("Longitude", strLongitude); 

Aber dann kommen sie mit:

latitude = bundle.getString("latitude"); 
longitude = bundle.getString("longitude"); 

Hinweis Sie mit einem Kapital setzen L, aber mit einem Klein l bekommen.

Verwandte Themen