我做一个地图程序,想实现定位地址功能,据我所知,应该是用geocoder这个类进行编码的。但是我运行的时候报SocketException: Address family not supported by protocol这个错误,哪位朋友能帮忙解答一下,感激!源代码如下:
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); // 从XML布局文件获得MapView对象 MapView mapView = (MapView) findViewById(R.id.mapview); // 允许通过触摸拖动地图 mapView.setClickable(true); // 当触摸地图时在地图下方会出现缩放按钮,几秒后就会消失 mapView.setBuiltInZoomControls(true); // 获得MapController对象,mapController是一个在类中定义的MapController类型变量 mapController = mapView.getController(); // 创建Geocoder对象,用于获得指定地点的地址 Geocoder gc = new Geocoder(this); // 将地图设为Traffic模式 mapView.setTraffic(true); try { // 查询指定地点的地址 List<Address> addresses = gc.getFromLocationName("沈阳三好街", 5); // 根据经纬度创建GeoPoint对象 geoPoint = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); setTitle(addresses.get(0).getFeatureName()); } catch (Exception e) { } // 创建MyOverlay对象,用于在地图上绘制图形 MyOverlay myOverlay = new MyOverlay(); // 将MyOverlay对象添加到MapView组件中 mapView.getOverlays().add(myOverlay); // 设置地图的初始大小,范围在1和21之间。1:最小尺寸,21:最大尺寸 mapController.setZoom(20); // 以动画方式进行定位 mapController.animateTo(geoPoint);}// 用于在地图上绘制图形的MyOverlay对象class MyOverlay extends Overlay{ @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { Paint paint = new Paint(); paint.setColor(Color.RED); Point screenPoint = new Point(); // 将“沈阳三好街”在地图上的位置转换成屏幕的实际坐标 mapView.getProjection().toPixels(geoPoint, screenPoint); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flag); // 在地图上绘制图像 canvas.drawBitmap(bmp, screenPoint.x, screenPoint.y, paint); // 在地图上绘制文字 canvas.drawText("三好街",screenPoint.x, screenPoint.y, paint); return super.draw(canvas, mapView, shadow, when); }}/***********************************************
注: geoPoint = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6));mapController.animateTo(geoPoint);应该是这里的问题,但是我不知道如何解决。
*************************************************/
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); // 从XML布局文件获得MapView对象 MapView mapView = (MapView) findViewById(R.id.mapview); // 允许通过触摸拖动地图 mapView.setClickable(true); // 当触摸地图时在地图下方会出现缩放按钮,几秒后就会消失 mapView.setBuiltInZoomControls(true); // 获得MapController对象,mapController是一个在类中定义的MapController类型变量 mapController = mapView.getController(); // 创建Geocoder对象,用于获得指定地点的地址 Geocoder gc = new Geocoder(this); // 将地图设为Traffic模式 mapView.setTraffic(true); try { // 查询指定地点的地址 List<Address> addresses = gc.getFromLocationName("沈阳三好街", 5); // 根据经纬度创建GeoPoint对象 geoPoint = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); setTitle(addresses.get(0).getFeatureName()); } catch (Exception e) { } // 创建MyOverlay对象,用于在地图上绘制图形 MyOverlay myOverlay = new MyOverlay(); // 将MyOverlay对象添加到MapView组件中 mapView.getOverlays().add(myOverlay); // 设置地图的初始大小,范围在1和21之间。1:最小尺寸,21:最大尺寸 mapController.setZoom(20); // 以动画方式进行定位 mapController.animateTo(geoPoint);}// 用于在地图上绘制图形的MyOverlay对象class MyOverlay extends Overlay{ @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { Paint paint = new Paint(); paint.setColor(Color.RED); Point screenPoint = new Point(); // 将“沈阳三好街”在地图上的位置转换成屏幕的实际坐标 mapView.getProjection().toPixels(geoPoint, screenPoint); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flag); // 在地图上绘制图像 canvas.drawBitmap(bmp, screenPoint.x, screenPoint.y, paint); // 在地图上绘制文字 canvas.drawText("三好街",screenPoint.x, screenPoint.y, paint); return super.draw(canvas, mapView, shadow, when); }}/***********************************************
注: geoPoint = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6));mapController.animateTo(geoPoint);应该是这里的问题,但是我不知道如何解决。
*************************************************/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货