package com.jiuqi.njt.ui.route; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.graphics.Color; import android.text.Spanned; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.AMap.InfoWindowAdapter; import com.amap.api.maps.AMap.OnMarkerClickListener; import com.amap.api.maps.model.BitmapDescriptor; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.PolylineOptions; import com.amap.api.search.core.LatLonPoint; import com.amap.api.services.route.DrivePath; import com.amap.api.services.route.DriveRouteResult; import com.amap.api.services.route.DriveStep; import com.jiuqi.njt.R; public class DrivingRouteOverlay implements OnMarkerClickListener, InfoWindowAdapter { private Context mContext; private AMap mMap; private DriveRouteResult drivePath; private Map<Integer, Marker> markerMap; private int currentIndex = 0; private static int zoomLevel = 15; private List<DriveStep> steps; public DrivingRouteOverlay(Context mContext, AMap mMap, DriveRouteResult drivePath) { super(); this.mContext = mContext; this.mMap = mMap; this.drivePath = drivePath; List<DrivePath> path = drivePath.getPaths(); steps = path.get(0).getSteps(); this.mMap.setInfoWindowAdapter(this); markerMap = new HashMap<Integer, Marker>(); } /* * 规划路径, */ public void addMarkerLine() { try { if(null!=mMap){ Marker startMarker = mMap.addMarker((new MarkerOptions()) .position( new LatLng(drivePath.getStartPos().getLatitude(), drivePath.getStartPos().getLongitude())) .icon(BitmapDescriptorFactory.fromResource(R.drawable.start)) .title("0")); startMarker.showInfoWindow(); for (int i = 0; i < steps.size(); i++) { if (i != 0) { steps.get(i).getPolyline(); BitmapDescriptor icon = BitmapDescriptorFactory .fromResource(R.drawable.car); Marker tempMarker = mMap.addMarker((new MarkerOptions()) .position( new LatLng(steps.get(i).getPolyline().get(0) .getLatitude(), steps.get(i) .getPolyline().get(0).getLongitude())) .icon(icon).anchor(0.5f, 0.5f).title("" + i)); markerMap.put(i, tempMarker); } mMap.addPolyline((new PolylineOptions()) .addAll(convertArrList(steps.get(i).getPolyline())) .color(Color.argb(180, 54, 114, 227)).width(20.9F)); } Marker targerMarker = mMap.addMarker((new MarkerOptions()) .position( new LatLng(drivePath.getTargetPos().getLatitude(), drivePath.getTargetPos().getLongitude())) .icon(BitmapDescriptorFactory.fromResource(R.drawable.end)) .title("" + (steps.size()))); markerMap.put(steps.size(), targerMarker); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void removeFromMap() { // TODO Auto-generated method stub currentIndex = 0; mMap.clear(); } public boolean showPrePopInfo() { Log.e("ceshi shuj u ", currentIndex + ""); if (currentIndex > 0) { currentIndex--; Marker merker = markerMap.get(currentIndex); if (null != merker) { mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( merker.getPosition(), zoomLevel)); merker.showInfoWindow(); } } if (currentIndex == 0) { return false; } else { return true; } } public boolean showNextPopInfo() { if (currentIndex < steps.size()) { currentIndex++; Marker merker = markerMap.get(currentIndex); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( merker.getPosition(), zoomLevel)); merker.showInfoWindow(); } if (currentIndex == steps.size()) { return false; } else { return true; } } @Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); currentIndex = Integer.parseInt(marker.getTitle()); return false; } @Override public View getInfoWindow(Marker arg0) { // TODO Auto-generated method stub return null; } public View getInfoView(Context cnt, int index) { if (index < 0 || index > steps.size()) { return null; } LinearLayout ll_parents = new LinearLayout(cnt); ll_parents.setOrientation(LinearLayout.VERTICAL); ll_parents.setBackgroundResource(R.drawable.custom_info_bubble); LinearLayout ll_child1 = new LinearLayout(cnt); ll_child1.setOrientation(LinearLayout.HORIZONTAL); ll_child1.setGravity(Gravity.AXIS_PULL_BEFORE); TextView titleVw = new TextView(cnt); String spannedInfos[] = getSpannedInfo(index).toString() .split("\\n", 2); titleVw.setTextColor(Color.BLACK); titleVw.setText(AMapUtil.stringToSpan(spannedInfos[0])); titleVw.setPadding(3, 0, 0, 3); ll_child1.addView(titleVw, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); TextView backgroud = new TextView(cnt); backgroud.setBackgroundColor(Color.rgb(165, 166, 165)); backgroud.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, 1)); LinearLayout ll_child3 = new LinearLayout(cnt); ll_child3.setOrientation(LinearLayout.VERTICAL); TextView titleVwdown = new TextView(cnt); if (spannedInfos.length == 2) { ll_child3.addView(backgroud, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, 1)); titleVwdown.setText(AMapUtil.stringToSpan(spannedInfos[1])); titleVwdown.setTextColor(Color.rgb(82, 85, 82)); ll_child3.addView(titleVwdown, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); } LinearLayout ll_child2 = new LinearLayout(cnt); ll_child2.setOrientation(LinearLayout.HORIZONTAL); ll_child2.setGravity(Gravity.CENTER_HORIZONTAL); ll_parents.addView(ll_child1, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); ll_parents.addView(ll_child3, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, 1)); ll_parents.addView(ll_child2, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); return ll_parents; } @Override public View getInfoContents(Marker arg0) { int index = 0; try { index = Integer.parseInt(arg0.getTitle()); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.e("1111111111", "@@@@+" + index); return getInfoView(mContext, index); } /** * 获取popInfo描述 * * @param index * @return */ public Spanned getSpannedInfo(int index) { if (index == steps.size()) { StringBuffer buf = new StringBuffer(); buf.append(ChString.Arrive); buf.append(steps.get(steps.size() - 1).getRoad()); return AMapUtil.stringToSpan(buf.toString()); } return getCarInfo(index); } /** * 获取驾车popInfo描述 * 起点上面显示的框提醒 * @param index * @return */ public Spanned getCarInfo(int index) { String content = ""; DriveStep sss = steps.get(index); if (!AMapUtil.IsEmptyOrNullString(sss.getInstruction()) && !AMapUtil.IsEmptyOrNullString(sss.getAction())) { // content = sss.getInstruction() + " --> " + sss.getAction(); content = sss.getInstruction(); } else { content=sss.getInstruction(); } content = AMapUtil.colorFont(content, AMapUtil.HtmlGray); content += AMapUtil.makeHtmlNewLine(); // int temp = content.indexOf("行驶"); // content.replaceFirst("行驶", "行驶"+"\n"+ ChString.Meter); String[] temp = content.split("行驶"); if (temp.length > 1) { content = temp[0] + "行驶" + "\n" + String.format("%s%s", " " + ChString.About, temp[1]); } // content += String.format("%s%s", ChString.About, // AMapUtil.getFriendlyLength(sss.getDistance())); // content += String.format("%s%s", ChString.About,sss.getDistance() + // ChString.Meter); return AMapUtil.stringToSpan(content); } private ArrayList<LatLng> convertArrList( List<com.amap.api.services.core.LatLonPoint> polyline) { ArrayList<LatLng> lineShapes = new ArrayList<LatLng>(); for (com.amap.api.services.core.LatLonPoint point : polyline) { LatLng latLngTemp = SearchPointConvert(new LatLonPoint( point.getLatitude(), point.getLongitude())); lineShapes.add(latLngTemp); } return lineShapes; } /** * 工具方法,将搜索得到的LatLonPoint转成latLng 才能添加到地图上 * * @param latLonPoint * @return */ private LatLng SearchPointConvert(LatLonPoint latLonPoint) { return new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude()); } }