搜索了很多网上通过经度纬度取地址的方法, 都是用的 Geocoder 对象的 Geocoder.getFromLocation 函数, 下面有例子, 但是经过我的测试, 无法获取地址。
网上找了资料, 可以使用互联网取地址,如下所示 :
http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.134545,101.401201&sensor=false&language=zh-CN
不过这种方法就是会造成多连接一次互联网, 返回的xml数据还要解析, 用户体验不是太好。
//该函数无法获取地址 private String getLocationAddress(GeoPoint point, double iLatitu, double iLongti) { String add = ""; //Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.CHINA); try { //Thread.sleep(5000); //Location location = locManager.getLastKnownLocation(locManager.GPS_PROVIDER); //List<Address> listAddresses = geoCoder.getFromLocation( point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 10); //List places = geoCoder.getFromLocation(point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 5); List<Address> listAddresses = geoCoder.getFromLocation( iLatitu, iLongti, 1); List places = geoCoder.getFromLocation(iLatitu, iLongti, 1); //Thread.sleep(5000); //http://maps.googleapis.com/maps/api/geocode/xml?latlng=31.134545,121.401201&sensor=false&language=zh-CN if (listAddresses.size() > 0) { Address address = listAddresses.get(0); int maxLine = address.getMaxAddressLineIndex(); if (maxLine >= 2) { add = address.getAddressLine(1) + address.getAddressLine(2); } else { add = address.getAddressLine(1); } } } //catch (InterruptedException e) //{ // add = ""; // e.printStackTrace(); //} catch (IOException e) { add = ""; e.printStackTrace(); } return add; }
最终找了另外一种方法,就是使用 QSearchListener 对象, 然后实现 onGetReverseGeocoder 函数的内容即可取到地址。
QSearchListener listenerQsearch = new QSearchListener()
@Override
public void onGetReverseGeocoder(int resultCode, QPlaceMark addr)
下面为源码实现:
QSearchListener listenerQsearch = new QSearchListener() { @Override public void onGetReverseGeocoder(int resultCode, QPlaceMark addr) { if (resultCode == QSearchListener.KQRESULT_OK) { Toast.makeText( KQDKActivity.this, addr.address, Toast.LENGTH_LONG).show(); } else { Toast.makeText(KQDKActivity.this, "无结果", Toast.LENGTH_LONG).show(); } } @Override public void onGetPoiSearch(int resultCode, QPoiResult poiResult) { if (resultCode == QSearchListener.KQRESULT_OK) { } else { Toast.makeText(KQDKActivity.this, "无结果", Toast.LENGTH_LONG).show(); } } @Override public void onGetBusLineSearch(int resultCode, QBusLineInfo busLineInfo) { if (resultCode == QSearchListener.KQRESULT_OK) { } else Toast.makeText(KQDKActivity.this, "公交线路无结果", Toast.LENGTH_LONG).show(); } @Override public void onGetRouteSearchResult(int resultCode, QRouteSearchResult routeSearchResult) { // TODO Auto-generated method stub if (resultCode == QSearchListener.KQRESULT_OK) { int type = routeSearchResult.routeSearchResultType; if (type == QRouteSearchResult.KQROUTESEARCHRESULT_BUSROUTE) { QBusRoutePlan plan = (QBusRoutePlan) routeSearchResult.routeSearchResult; } else if (type == QRouteSearchResult.KQROUTESEARCHRESULT_DRIVEROUTE) { QDriveRouteInfo info = (QDriveRouteInfo) routeSearchResult.routeSearchResult; } else if (type == QRouteSearchResult.KQROUTESEARCHRESULT_BUSLIST) { Toast.makeText(KQDKActivity.this, "路线搜索结果为选择列表", Toast.LENGTH_LONG).show(); QRouteQueryResultChoice choice = (QRouteQueryResultChoice)routeSearchResult.routeSearchResult; mQsearch.setBusRoutePolicy(QSearch.KQBUS_ROUTE_POLICY_SHORTCUT); mQsearch.busRouteSearchWithLocation("北京", choice.startList.get(0), choice.endList.get(0)); } else if (type == QRouteSearchResult.KQROUTESEARCHRESULT_DRIVELIST) { Toast.makeText(KQDKActivity.this, "路线搜索结果为选择列表", Toast.LENGTH_LONG).show(); QRouteQueryResultChoice choice = (QRouteQueryResultChoice)routeSearchResult.routeSearchResult; mQsearch.setDrivingRoutePolicy(QSearch.KQDRIVING_ROUTE_POLICY_SHORTCUT); mQsearch.driveRouteSearchWithLocation("北京", choice.startList.get(0), "北京",choice.endList.get(0)); } } else Toast.makeText(KQDKActivity.this, "路线搜索无结果", Toast.LENGTH_LONG).show(); } @Override public void onGetSmartTrips(int resultCode, List<String> smartTrips) { // TODO Auto-generated method stub if (resultCode == QSearchListener.KQRESULT_OK) { String s = null; int num = smartTrips.size(); for (int i = 0; i < num; ++i) { if (s == null) { s = smartTrips.get(i); } else { s += "\n" + smartTrips.get(i); } } Toast.makeText(KQDKActivity.this, s, Toast.LENGTH_LONG).show(); } else { Toast.makeText(KQDKActivity.this, "路线搜索无结果", Toast.LENGTH_LONG).show(); } } @Override public void onGetGeocoder(int resultCode, QGeocoderInfo geocodeInfo) { // TODO Auto-generated method stub if (resultCode == QSearchListener.KQRESULT_OK) { String s = geocodeInfo.province + "\n" + geocodeInfo.city + "\n" + geocodeInfo.district; Toast.makeText(KQDKActivity.this, s, Toast.LENGTH_LONG).show(); } else { Toast.makeText(KQDKActivity.this, "无结果", Toast.LENGTH_LONG).show(); } } };