在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系统中需要的google play services等另行查看资料,沦落凡间不详述。
参考:https://developers.google.com/maps/documentation/android/intro
首先我们在Activity上加载一个GoogleMap,然后再在Map上画上标记和路径导航。
先上主要代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class DirectionActivity extends FragmentActivity { 2 private Button btnBack; 3 private SupportMapFragment mapFragment; 4 private GoogleMap map; 5 private LocationManager locationManager; 6 private Location location; 7 private List<LatLng> latlngs = new ArrayList<LatLng>(); 8 private double lat; 9 private double lng; 10 private String address; 11 private double locLat; 12 private double locLng; 13 private LatLng locLatLng; 14 15 16 @Override 17 protected void onCreate(Bundle arg0) { 18 super.onCreate(arg0); 19 init(); 20 findViewById(); 21 setListenter(); 22 processLogic(); 23 getDirection(); 24 } 25 26 /** 27 * Get the direction 28 */ 29 private void getDirection() { 30 FoundRoadRouteTask task = new FoundRoadRouteTask(onFoundRoadRouteTaskListener); 31 task.execute(new String[] {locLat+","+locLng, lat + "," + lng }); 32 } 33 34 /** 35 * Draw route information 36 */ 37 private void drawRoute() { 38 try { 39 // Draw route info 40 map.addMarker(new MarkerOptions().position(latlngs.get(0)).title(address).visible(true)); 41 PolylineOptions lineOptions = new PolylineOptions(); 42 lineOptions.width(5); 43 for (int i = 0; i < latlngs.size() - 1; i++) { 44 lineOptions.add(latlngs.get(i)); 45 } 46 map.addPolyline(lineOptions); 47 } catch (Exception e) { 48 49 } 50 } 51 52 private OnFounedRoadRouteListener onFoundRoadRouteTaskListener = new OnFounedRoadRouteListener() { 53 54 @SuppressWarnings("unchecked") 55 @Override 56 public void found(Object obj) { 57 if (obj != null) { 58 List<LatLng> tempLats = (List<LatLng>) obj; 59 latlngs.addAll(tempLats); 60 } 61 drawRoute(); 62 } 63 }; 64 65 private void getLatLng() { 66 try { 67 Intent latlngIntent = getIntent(); 68 Bundle b = latlngIntent.getBundleExtra("latlng"); 69 lat = b.getDouble("lat"); 70 lng = b.getDouble("lng"); 71 address = b.getString("address"); 72 } catch (Exception e) { 73 74 } 75 } 76 77 protected void init() { 78 setContentView(R.layout.direction_layout); 79 getLatLng(); 80 btnBack = (Button) this.findViewById(R.direction_layout.btn_back); 81 } 82 83 protected void processLogic() { 84 85 locLat=application.getLatitude(); 86 locLng=application.getLongitude(); 87 88 FragmentManager manager = getSupportFragmentManager(); 89 mapFragment = (SupportMapFragment) manager 90 .findFragmentById(R.direction_layout.map); 91 map = mapFragment.getMap(); 92 initMap(); 93 } 94 95 protected void setListenter() { 96 btnBack.setOnClickListener(onBackClickListener); 97 } 98 99 /** 100 * Initialize the map 101 */ 102 private void initMap() { 103 try { 104 //设置在地图上显示手机的位置,和显示控制按钮 105 map.setMyLocationEnabled(true); 106 //设置室内地图是否开启 107 map.setIndoorEnabled(true); 108 //设置地图类型三种:1:MAP_TYPE_NORMAL: Basic map with roads. 109 //2:MAP_TYPE_SATELLITE: Satellite view with roads.3:MAP_TYPE_TERRAIN: Terrain view without roads. 110 map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 111 // map.setTrafficEnabled(true); 112 map.setOnMapLongClickListener(onMapLongClickListener); 113 114 GPSUtil gpsUtil = new GPSUtil(); 115 gpsUtil.startLocation(this, getContentResolver(), 116 onLocationChangedListener); 117 118 locationManager = (LocationManager) this 119 .getSystemService(Context.LOCATION_SERVICE); 120 location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 121 } catch (Exception e) { 122 123 } 124 } 125 126 /** On Location change listener */ 127 private OnLocationChangedListener onLocationChangedListener = new OnLocationChangedListener() { 128 129 @Override 130 public void onLocationChanged(Location location) { 131 // To get the local latitude and longitude and set the map view to 132 // the position 133 if (location != null) { 134 locLat = location.getLatitude(); 135 locLng = location.getLongitude(); 136 locLatLng = new LatLng(locLat, locLng); 137 map.moveCamera(CameraUpdateFactory.newLatLng(locLatLng)); 138 map.moveCamera(CameraUpdateFactory.zoomTo(15)); 139 } 140 } 141 }; 142 143 /** On Map long click listener */ 144 private OnMapLongClickListener onMapLongClickListener = new OnMapLongClickListener() { 145 146 @Override 147 public void onMapLongClick(LatLng latlng) { 148 Location location = new Location("LongPressLocationProvider"); 149 location.setLatitude(latlng.latitude); 150 location.setLongitude(latlng.longitude); 151 //设置精度 152 location.setAccuracy(20); 153 onLocationChangedListener.onLocationChanged(location); 154 } 155 }; 156 157 private OnClickListener onBackClickListener = new OnClickListener() { 158 159 @Override 160 public void onClick(View v) { 161 DirectionActivity.this.finish(); 162 } 163 }; 164 }
分析:
1 private void getLatLng() { 2 try { 3 Intent latlngIntent = getIntent(); 4 Bundle b = latlngIntent.getBundleExtra("latlng"); 5 lat = b.getDouble("lat"); 6 lng = b.getDouble("lng"); 7 address = b.getString("address"); 8 } catch (Exception e) { 9
10 } 11 }
在这个方法中我们从intent中拿到启动这个fragmentActivity时传过来的目的地经纬度。
1 protected void processLogic() { 2 3 locLat=application.getLatitude(); 4 locLng=application.getLongitude(); 5 6 FragmentManager manager = getSupportFragmentManager(); 7 mapFragment = (SupportMapFragment) manager 8 .findFragmentById(R.direction_layout.map); 9 map = mapFragment.getMap(); 10 initMap(); 11 }
这里的3,4行为拿到当前本地的经纬度,application为一个Application类的对象,里面写有get方法,可以拿到当前本地经纬度。6,7行为得到mapFragment对象。9行为通过mapFragment获得GoogleMap对象,后面我们就是用这个map对象来画标记和线路。
1 private void initMap() { 2 try { 3 //设置在地图上显示手机的位置,和显示控制按钮 4 map.setMyLocationEnabled(true); 5 //设置室内地图是否开启 6 map.setIndoorEnabled(true); 7 //设置地图类型三种:1:MAP_TYPE_NORMAL: Basic map with roads. 8 //2:MAP_TYPE_SATELLITE: Satellite view with roads.3:MAP_TYPE_TERRAIN: Terrain view without roads. 9 map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 10 // map.setTrafficEnabled(true); 11 map.setOnMapLongClickListener(onMapLongClickListener); 12 13 GPSUtil gpsUtil = new GPSUtil(); 14 gpsUtil.startLocation(this, getContentResolver(), 15 onLocationChangedListener); 16 17 locationManager = (LocationManager) this 18 .getSystemService(Context.LOCATION_SERVICE); 19 location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 20 } catch (Exception e) { 21
22 } 23 }
这个方法用来初始化地图,3-11行为地图的设置,更多请查看:http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html 。13-19行的GPSUtil为一个监听本地位置改变的类,当手机位置移动时GPS会得到移动后的经纬度。
1 private OnLocationChangedListener onLocationChangedListener = new OnLocationChangedListener() { 2 3 @Override 4 public void onLocationChanged(Location location) { 5 // To get the local latitude and longitude and set the map view to 6 // the position 7 if (location != null) { 8 locLat = location.getLatitude(); 9 locLng = location.getLongitude(); 10 locLatLng = new LatLng(locLat, locLng); 11 map.moveCamera(CameraUpdateFactory.newLatLng(locLatLng)); 12 map.moveCamera(CameraUpdateFactory.zoomTo(15)); 13 } 14 } 15 };
这个匿名类里的方法设置了当位置改变后地图上指示点和镜头的变化,拿到经纬度后用一个LatLng类的对象来存放,11,22行设置视角的移动,moveCamera方法传入的是一个CameraUpdate对象,该对象可用CameraUpdateFactory来生成,传入CameraUpdateFactory.newLatLng(locLatLng)移动到这个新的经纬度的位置,CameraUpdateFactory.zoomTo(15)设置移动后视角镜头大小。
1 private void getDirection() { 2 FoundRoadRouteTask task = new FoundRoadRouteTask(onFoundRoadRouteTaskListener); 3 task.execute(new String[] {locLat+","+locLng, lat + "," + lng }); 4 }
这个方法为启动一个异步线程处理向gooogle服务器请求路线规划,传入起点和目的地的经纬度。FoundRoadRouteTask继承了AsyncTask。等下讲请求的地址和处理方法。
1 private OnFounedRoadRouteListener onFoundRoadRouteTaskListener = new OnFounedRoadRouteListener() { 2 3 @SuppressWarnings("unchecked") 4 @Override 5 public void found(Object obj) { 6 if (obj != null) { 7 List<LatLng> tempLats = (List<LatLng>) obj; 8 latlngs.addAll(tempLats); 9 } 10 drawRoute(); 11 } 12 };
从这个回调方法中得到处理好的LatLng对象,里面存放了用来画出线路的所有点的经纬度(把点连成线)。
1 /** 2 * Draw route information 3 */ 4 private void drawRoute() { 5 try { 6 // Draw route info 7 map.addMarker(new MarkerOptions().position(latlngs.get(0)).title(address).visible(true)); 8 PolylineOptions lineOptions = new PolylineOptions(); 9 lineOptions.width(5); 10 for (int i = 0; i < latlngs.size() - 1; i++) { 11 lineOptions.add(latlngs.get(i)); 12 } 13 map.addPolyline(lineOptions); 14 } catch (Exception e) { 15
16 } 17 }
这个方法就是画出线路的主要方法了,第7行在地图上标记出手机的当前位置,通过GoogleMap的addMarker方法,传入一个MarkerOptions类型的对象,该对象设置了标记的信息,如:.position(LatLng latlng)设置地点,.title(String address)设置标记显示的地点名称,.visible(boolean b)设置标记是否可见。我们可以在map上画线段[addPolyline(PolylineOptions options)],多边形[addPolygon(PolygonOptions options)],圆[addCircle(CircleOptions options)],图像[addGroundOverlay(GroundOverlayOptions options)]。传入的都是一个PolygonOptions类型的对象,该对象定义了你所画的图形,我们画线路导航所以用addPolyline(),并使用PolygonOptions.add(LatLng... points)方法添加多个点的坐标(7-12行),lineOptions.width(5);设置了所画线的宽度(自行调整数值),最后用GoogleMap对象的addPolyline()方法画出路线导航。
下面是请求和处理路线坐标经纬度的方法:
1:首先我们向这个地此发送http请求(这个应该没问题吧):http://maps.google.com/maps/api/directions/xml?origin=[lat],[lng]&destination=[lat],[lng]&sensor=false&mode=driving
地址中的origin=[lat],[lng]为起始地的纬度和经度,destination=[lat],[lng]为目的地的纬度和经度(用double类型的经纬度数据替换中括号),xml为设置返回的数据格式为xml格式,也可以写为json。mode=driving为设置出行模式了驾驶,还有步行等方式,详细可参考首段中的api文档地址。
2:处理http请求返回的字符串(String strResult),在<overview_polyline>标签中有编码的线路导航坐标点经纬度,且已经接近于平滑,方法如下:
List<LatLng> points=new ArrayList<LatLng>();
if (-1 == strResult.indexOf("<status>OK</status>")){ return null; } int pos = strResult.indexOf("<overview_polyline>"); pos = strResult.indexOf("<points>", pos + 1); int pos2 = strResult.indexOf("</points>", pos); strResult = strResult.substring(pos + 8, pos2); List<GeoPoint> geoPoints = decodePoly(strResult); // Log.i("tag", "geoPoints:"+geoPoints.toString()); LatLng ll; Log.i("tag", "geopoints.size:"+geoPoints.size()); for(Iterator<GeoPoint> gpit=geoPoints.iterator();gpit.hasNext();){ GeoPoint gp=gpit.next(); double latitude=gp.getLatitudeE6(); latitude=latitude/1000000; // Log.i("tag", "latitude:"+latitude); double longitude=gp.getLongitudeE6(); longitude=longitude/1000000; // Log.i("tag", "longitude:"+longitude); ll=new LatLng(latitude, longitude); points.add(ll); } // Log.i("tag", "points:"+points.toString()); return points;
1 /** 2 * Parses the returned lines of code of XML overview_polyline 3 * 4 * @return List<GeoPoint> 5 */ 6 private List<GeoPoint> decodePoly(String encoded) { 7 8 List<GeoPoint> poly = new ArrayList<GeoPoint>(); 9 int index = 0, len = encoded.length(); 10 int lat = 0, lng = 0; 11 12 while (index < len) { 13 int b, shift = 0, result = 0; 14 do { 15 b = encoded.charAt(index++) - 63; 16 result |= (b & 0x1f) << shift; 17 shift += 5; 18 } while (b >= 0x20); 19 int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 20 lat += dlat; 21 22 shift = 0; 23 result = 0; 24 do { 25 b = encoded.charAt(index++) - 63; 26 result |= (b & 0x1f) << shift; 27 shift += 5; 28 } while (b >= 0x20); 29 int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 30 lng += dlng; 31 32 GeoPoint p = new GeoPoint((int) ((lat / 1E5) * 1E6), 33 (int) ((lng / 1E5) * 1E6)); 34 poly.add(p); 35 } 36 37 return poly; 38 }
这样返回的列表points中存放的点即为我们要用来画线路的坐标点了。
到此android用google map api v2画线路导航的方法就讲完了,沦落凡间 已使用没有问题,有疑问可以在下面提出来。