使用Android模拟器2.2 api 8 我一直得到IOException
03-05 19:42:11.073: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
03-05 19:42:15.505: WARN/System.err(1823): java.io.IOException: Service not Available
这是我的代码:
private LocationManager manager = null;
LocationListener locationListener = null;
double latitude = 0;
double longtitude = 0;
List<Address> myList = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// httpTranslateGet();
try
{
manager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
initLocationListener();
manager.requestLocationUpdates(manager.GPS_PROVIDER, 0, 0,
locationListener);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initLocationListener()
{
locationListener = new LocationListener()
{
@Override
public void onLocationChanged(android.location.Location location)
{
if (location != null)
{
latitude = location.getLatitude();
longtitude = location.getLongitude();
try
{
Geocoder geocoder = new Geocoder(WeatherCastDemo.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
myList = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(), 10);
StringBuilder sb = new StringBuilder();
if (myList.size() > 0)
{
Address address = myList.get(0);
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());
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
public void onProviderDisabled(String arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
};
}
任何人都有任何想法?
我已经设法使用Emulator 2.1 api 7,但是反向地理编码总是给出一个空的结果。有人可以确认我的代码吗?
感谢。
感谢, 射线。
答案 0 :(得分:21)
这是模拟器的已知问题。 它在实际设备上运行良好
在2.2 API 8上,您将收到以下stacktrace
java.io.IOException: Service not Available
at android.location.Geocoder.getFromLocation(Geocoder.java:117)
有关详细信息(以及可能的解决方法),请参阅此处,请参阅以下网址:
http://code.google.com/p/android/issues/detail?id=8816
如果您在较低的API上使用GeoCoder时遇到问题,则应检查堆栈跟踪。我不时有以下内容:
java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:124)
这可以是Google的服务器端问题,也可以是客户端问题(互联网连接)。
如果GeoCoder返回一个空列表,您需要检查设备(仿真器或真实电话)上是否有适当的GeoCoder实现。
这可以使用Geocoder对象上的isPresent()方法完成。
http://developer.android.com/reference/android/location/Geocoder.html
此外,在模拟器上运行时,请确保使用Google API设置AVD图像。
答案 1 :(得分:10)
您可以通过以下方式使用 Google Place API
创建一个方法,返回带有HTTP调用响应的JSONObject
,如下所示
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
现在将JSONObject传递给 getLatLong()方法,如下所示
public static GeoPoint getLatLong(JSONObject jsonObject) {
Double lon = new Double(0);
Double lat = new Double(0);
try {
lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (Exception e) {
e.printStackTrace();
}
return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
}
这是在API级别8上工作和测试...跳这个帮助..
答案 2 :(得分:4)
我读了@ddewaele提到的讨论主题,有人说重启可以解决问题。它做了。顺便说一句,我的设备的Android版本是4.1。
答案 3 :(得分:2)
纬度和经度是您各自的值
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses.size() > 0) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder(
"Address:\n");
for (int i = 0; i < returnedAddress
.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(
returnedAddress.getAddressLine(i)).append("\n");
}
adrs.setText(strReturnedAddress.toString());
} else {
adrs.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 4 :(得分:1)
上面的代码稍有变化,允许替换某些终端似乎不支持的getFromLocationName调用:
private static List<Address> getAddrByWeb(JSONObject jsonObject){
List<Address> res = new ArrayList<Address>();
try
{
JSONArray array = (JSONArray) jsonObject.get("results");
for (int i = 0; i < array.length(); i++)
{
Double lon = new Double(0);
Double lat = new Double(0);
String name = "";
try
{
lon = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
lat = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
name = array.getJSONObject(i).getString("formatted_address");
Address addr = new Address(Locale.getDefault());
addr.setLatitude(lat);
addr.setLongitude(lon);
addr.setAddressLine(0, name != null ? name : "");
res.add(addr);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return res;
}
}
答案 5 :(得分:0)
你可以使用Mr.cyclopes49给出的代码作为两个函数,并在getLatLong()
方法中添加函数调用getLocationInfo()
和onCreate()
它的工作原理示例语句是
JSONObject jo = this.getLocationInfo("vizianagaram");
GeoPoint p=this.getLatLong(jo);
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
这很好用!
答案 6 :(得分:0)
fun getPostalCode(context: Context, latitude: Double, longitude: Double) : String
{
val geocoder = Geocoder(context, Locale.getDefault())
var postalCode = ""
try {
val address = geocoder.getFromLocation(latitude, longitude, 1)
if (address != null && address[0] != null && address[0].postalCode != null)
postalCode = address.get(0).postalCode
}
catch (e: IOException) {
Log.e(TAG, e.toString())
}
return postalCode
}