mapabc V5.1.1不支持android4.0的功能(路径规划,周边搜索的接口)。
android4.0可以用Google的HTTP服务下载XML,解析,然后自己画线。
Google自驾的HTTP服务:http://maps.google.com/maps/api/directions/xml?origin=22.538646,113.945144&destination=22.543416,114.118352&sensor=false&mode=driving
String strResult = getRouteXML(fromPoint, toPoint);
if (-1 == strResult.indexOf("<status>OK</status>")){
Toast.makeText(this, "生成路径失败!", Toast.LENGTH_SHORT).show();
return;
}
MyRoute mRoute = parseRouteXML(strResult);
List<GeoPoint> points = decodePoly(mRoute.points);
OverLayForAndroidFour mOverlay = new OverLayForAndroidFour(points,this,node.m_pass_seq,i,mRoute.step);
routeOverlayListForA4.add(mOverlay);(将此Oerlay添加到地图上就OK了)
private List<GeoPoint> decodePoly(String encoded) {
List<GeoPoint> poly = new ArrayList<GeoPoint>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),
(int) (((double) lng / 1E5) * 1E6));
poly.add(p);
}
return poly;
}
private String getRouteXML(GeoPoint fromPoint, GeoPoint toPoint){
String url = "http://maps.google.com/maps/api/directions/xml?origin="+fromPoint.getLatitudeE6()/1E6+","+ fromPoint.getLongitudeE6()/1E6+
"&destination="+toPoint.getLatitudeE6()/1E6+","+ toPoint.getLongitudeE6()/1E6+"&sensor=false&mode=driving";
System.out.println(url);
HttpGet get = new HttpGet(url);
String strResult = "";
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(get);
if (httpResponse.getStatusLine().getStatusCode() == 200){
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
}
return strResult;
}
private MyRoute parseRouteXML(String resultStr){
final MyRoute mRoute = new MyRoute();
InputStream inputStream = new ByteArrayInputStream(resultStr.getBytes());
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser;
XMLReader reader = null;
try
{
parser = factory.newSAXParser();
reader = parser.getXMLReader();
} catch (ParserConfigurationException e)
{
e.printStackTrace();
} catch (SAXException e)
{
e.printStackTrace();
}
RootElement directionsResponseElement = new RootElement("DirectionsResponse");
android.sax.Element routeElement = directionsResponseElement.getChild("route");
android.sax.Element overview_polylineElement = routeElement.getChild("overview_polyline");
android.sax.Element pointsElement = overview_polylineElement.getChild("points");
pointsElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
mRoute.points = vip;
}
});
android.sax.Element legElement = routeElement.getChild("leg");
android.sax.Element distanceElement = legElement.getChild("distance");
android.sax.Element valueElement = distanceElement.getChild("value");
valueElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
mRoute.distance = Integer.parseInt(vip);
}
});
android.sax.Element stepElement = legElement.getChild("step");
stepElement.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
mRoute.lat = "";
mRoute.lng = "";
}
});
//读到元素结束位置时触发,如读到</rec>时
stepElement.setEndElementListener(new EndElementListener() {
@Override
public void end() {
GeoPoint step = new GeoPoint((int)(Float.parseFloat(mRoute.lat) * 1E6),(int)(Float.parseFloat(mRoute.lng) * 1E6));
mRoute.step.add(step);
}
});
android.sax.Element start_locationElement = stepElement.getChild("start_location");
android.sax.Element latElement = start_locationElement.getChild("lat");
latElement.setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String vip) {
mRoute.lat = vip;
}
});
android.sax.Element lngElement = start_locationElement.getChild("lng");
lngElement.setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String vip) {
mRoute.lng = vip;
}
});
if (reader != null)
{
reader.setContentHandler(directionsResponseElement.getContentHandler());
try
{
reader.parse(new InputSource(inputStream));
} catch (IOException e)
{
e.printStackTrace();
} catch (SAXException e)
{
e.printStackTrace();
}
}
return mRoute;
}
//画线的主要类
public class OverLayForAndroidFour extends Overlay {
private List<GeoPoint> points;
private List<GeoPoint> step;
private Paint paint;
private Context context ;
private int resId;
private int i;
public OverLayForAndroidFour(List<GeoPoint> points, Context context, int resId, int i,List<GeoPoint> step) {
this.points = points;
this.context = context;
this.resId = resId;
this.i = i;
paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(150);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(5);
this.step = step;
}
private int myBitMapResId(int seq){
int id=-1;
switch(seq){
case 0:
id=R.drawable.point_start_b;
break;
case 1:
id=R.drawable.point_passby_1;
break;
case 2:
id=R.drawable.point_passby_2;
break;
case 3:
id=R.drawable.point_passby_3;
break;
case 4:
id=R.drawable.point_passby_4;
break;
case 5:
id=R.drawable.point_passby_5;
break;
case 6:
id=R.drawable.point_passby_6;
break;
case 7:
id=R.drawable.point_passby_7;
break;
case 8:
id=R.drawable.point_passby_8;
break;
case 9:
id=R.drawable.point_end_b;
break;
}
return id;
}
private Bitmap b1,b2;
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
if(b1 != null){
if(!b1.isRecycled() ){
b1.recycle(); //回收图片所占的内存
//System.gc(); //提醒系统及时回收
}
}
if(b2 != null){
if(!b2.isRecycled() ){
b2.recycle(); //回收图片所占的内存
//System.gc(); //提醒系统及时回收
}
}
Projection projection = mapView.getProjection();
if (points != null && points.size() >= 2) {
Point start = new Point();
projection.toPixels(points.get(0), start);
if(i == 1){
InputStream is = context.getResources().openRawResource(R.drawable.point_start_b);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = false;
b1 =BitmapFactory.decodeStream(is,null,options);
canvas.drawBitmap(b1, start.x-24, start.y - 66, null);
}
for (int i = 1; i < points.size(); i++) {
Point end = new Point();
projection.toPixels(points.get(i), end);
canvas.drawLine(start.x, start.y, end.x, end.y, paint);
start = end;
if(i == points.size()-1){
InputStream is = context.getResources().openRawResource(myBitMapResId(resId));
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = false;
b2 =BitmapFactory.decodeStream(is,null,options);
canvas.drawBitmap(b2, start.x-24, start.y - 66, null);
}
}
}
}
}
}