我正在开发一个在用户输入地址后使用纬度和经度的应用程序。我正在使用Geocoder从输入地址获取经度和纬度。但问题是从Geocoder返回的相关地址,但我只能阅读其中一个。以下是我正在使用的代码:
list=list(a='This place is awesome', b='I failed in the exam')
lapply(list, polarity)
在上述情况下,我能够获得自由,但无法获得经度。调试后我知道这个。现在当我交换它们像
Geocoder geocoder = new Geocoder(this);
List<Address> addresses;
double[] cordinates = new double[2];
try {
addresses = geocoder.getFromLocationName(locName, 1);
if (addresses.size() > 0) {
cordinates[0] = addresses.get(0).getLatitude();
cordinates[1] = addresses.get(0).getLongitude();//unable to get this one
return cordinates;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
现在我得到经度而不是纬度。 这有什么问题?
答案 0 :(得分:1)
试试这个有效的代码:
AutoCompleteTextView acGooglePlaces = (AutoCompleteTextView) findViewById(R.id.ac_edit_my_event_places);
acGooglePlaces.setAdapter(new GooglePlacesAutocompleteAdapter(GooglePlaces.this, R.layout.auto_complete_text_layout));
acGooglePlaces.requestFocus();
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
List<Address> returnedaddresses = geoCoder.getFromLocationName(acGooglePlaces.getText().toString(),1);
if(!returnedaddresses.isEmpty()){
String latForVol = String.valueOf(returnedaddresses.get(0).getLatitude());
String longForVol = String.valueOf(returnedaddresses.get(0).getLongitude());
Log.e("Lat", latForVol);
Log.e("Long", longForVol);
Log.e("Location", acGooglePlaces.getText().toString());
}else {
Log.e("Check", "Please give the correct address");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
答案 1 :(得分:1)
这很好用。由于您在遇到中断时使用断点调试代码
return cordinates;
它去了
return null;
它只在调试时发生。它始终返回正确的值。您可以通过插入日志语句来检查返回的值。
{
.....
double[] coordinates = getLongLat("Your address");
Log.wtf(TAG,"Lat:"+coordinates[0]+" Long:"+coordinates[1]);// This will log the correct values
.....
}
public double[] getLongLat(String address){
Geocoder geocoder = new Geocoder(this);
List<Address> addresses;
double[] cordinates = new double[2];
try {
addresses = geocoder.getFromLocationName(address, 1);
if (addresses.size() > 0) {
Address address1 = addresses.get(0);
cordinates[0] = address1.getLatitude();
cordinates[1] = address1.getLongitude();
return cordinates;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
答案 2 :(得分:0)
public static LatLng reverseGeocoding(Context context, String locationName){
if(!Geocoder.isPresent()){
Log.w("zebia", "Geocoder implementation not present !");
}
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(locationName, 1);
} catch (IOException e) {
Log.d(Geocoding.class.getName(), "not possible finding LatLng for Address : " + locationName);
}
if(addresses.size() > 0){
Log.d("zebia", "reverse Geocoding : locationName " + locationName + "Latitude " + addresses.get(0).getLatitude() );
return new LatLng(addresses.get(0).getLatitude(), addresses.get(0).getLongitude());
}else{
//use http api
}
return null;
}