问题描述
我试图将地理编码器值从MapFragment传递到LocationDetailsActivity。
我得到lat和lng的正确值,但是当我尝试显示任何其他值时,大部分时间都是 null价值 而不是城市和邮政编码(但并非总是),而很多时候我获得了正确的国家和州(并非总是)。
MapFragment代码:
//设置默认的纬度和经度
double latitude = 15.4825766;
double longitude = -5.0076589;
//获取当前位置和LatLng对象的经度和纬度
if(myLocation!= null){
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
}
mMap.setOnMapLongClickListener(新GoogleMap.OnMapLongClickListener(){
@覆盖
公共无效onMapLongClick(经纬度为arg0){
地址解析器地址解析器=新地址解析器(getActivity(),Locale.getDefault());
尝试{
列表与LT;地址> allAddresses = geocoder.getFromLocation(arg0.latitude,arg0.longitude,1 );
if(allAddresses.size()> 0&& allAddresses!= null){
Address address = allAddresses.get(0);
Intent intent = new Intent (),getActivity(),LocationDetailsActivity.class);
intent.putExtra(latitude,arg0.latitude);
intent.putExtra(longitude,arg0.longitude);
intent.putExtra (city,allAddresses.get(0).getLocality());
intent.putExtra(zip,allAddresses.get(0).getPostalCode());
intent.putExtra(州, allAddresses.get(0).getAdminArea());
intent.putExtra(country,allAddresses.get(0).getCountryName());
startActivity(intent);
}
} catch(IOException e){
e.printStackTrace();
}
}
});
LocationDetailsActivity代码:
Bundle bundle = getIntent()。getExtras();
double lat = bundle.getDouble(latitude);
double lng = bundle.getDouble(longitude);
String city = intent.getStringExtra(city);
String zip = intent.getStringExtra(zip);
String state = intent.getStringExtra(state);
String country = intent.getStringExtra(country);
//我在这里显示我的值
mFirstValueDisplay.setText(String.valueOf(city));
mSecondValueDisplay.setText(String.valueOf(zip));
Android的地理编码API对我的经验来说相当不可靠,我通常会向Url上的Google地理编码Web服务发送请求:https://maps.googleapis.com/maps/api/geocode
(If你熟悉翻新)
$ b $ pre $ @GET(/ json)
void reverseGeoCode(@Query(latlng )字符串latlng,@Query(language)字符串语言,
@Query(key)字符串键,回调< ReverseGeoCode>回调);
latlng 您想要反转地理编码的经度和纬度。
语言地理编码响应的语言
键您的Api键
I am trying to pass geocoder values from MapFragment to LocationDetailsActivity.
I get the correct values for lat and lng, but when I try to display any of the other values, most of the times I get null values instead of the city and zip (but not always), while a lot of the times I get the correct country and state (also not always).
MapFragment Code:
// Set default latitude and longitude
double latitude = 15.4825766;
double longitude = -5.0076589;
// Get latitude and longitude of the current location and a LatLng object
if (myLocation != null) {
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
}
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
try {
List<Address> allAddresses = geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1);
if (allAddresses.size() > 0 && allAddresses != null) {
Address address = allAddresses.get(0);
Intent intent = new Intent(getActivity(), LocationDetailsActivity.class);
intent.putExtra("latitude", arg0.latitude);
intent.putExtra("longitude", arg0.longitude);
intent.putExtra("city", allAddresses.get(0).getLocality());
intent.putExtra("zip", allAddresses.get(0).getPostalCode());
intent.putExtra("state", allAddresses.get(0).getAdminArea());
intent.putExtra("country", allAddresses.get(0).getCountryName());
startActivity(intent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
LocationDetailsActivity Code:
Bundle bundle = getIntent().getExtras();
double lat = bundle.getDouble("latitude");
double lng = bundle.getDouble("longitude");
String city = intent.getStringExtra("city");
String zip = intent.getStringExtra("zip");
String state = intent.getStringExtra("state");
String country = intent.getStringExtra("country");
// I display my values here
mFirstValueDisplay.setText(String.valueOf(city));
mSecondValueDisplay.setText(String.valueOf(zip));
Android's geocoding api is pretty unreliable up-to my experience, I usually make a request to the Google's geocoding webservices on Url : "https://maps.googleapis.com/maps/api/geocode"
(If you are familiar with retrofit)
@GET("/json")
void reverseGeoCode(@Query("latlng") String latlng, @Query("language") String language,
@Query("key") String key, Callback<ReverseGeoCode> callback);
latlng The latitude and longitude you want to reverse geocode.
language language of the geocoded response
key Your Api key
这篇关于地理编码器通常会在Android中返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!