我正在为谷歌地图上的方向做一个应用程序。但是当我运行该应用程序时,我遇到了一个问题11-23 09:39:16.111: E/MapActivity(572): Couldn't get connection factory client
,它没有在 Map 上显示/绘制 des 和 source。你能帮助我吗!
源代码
主要:
public class MainActivity extends MapActivity {
private MapView mapView;
@Override
protected boolean isRouteDisplayed() { return false; }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapview);
//Example data
double latitudeFrom = 50.06469036372934;
double longitudeFrom = 19.944788217544556;
double latitudeTo = 50.06400165088368;
double longitudeTo = 19.920390844345093;
GeoPoint srcGeoPoint =
new GeoPoint((int)(latitudeFrom * 1E6), (int)(longitudeFrom * 1E6));
GeoPoint destGeoPoint =
new GeoPoint((int)(latitudeTo * 1E6), (int)(longitudeTo * 1E6));
drawPath(srcGeoPoint, destGeoPoint, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
private void drawPath(GeoPoint src, GeoPoint dest, MapView mapView) {
String strUrl = "http://maps.google.com/maps?f=d&hl=en";
//From
strUrl += "saddr=" +
(src.getLatitudeE6()/1.0E6) +
"," +
(src.getLongitudeE6()/1.0E6);
//To
strUrl += "&daddr=" +
(dest.getLatitudeE6()/1.0E6) +
"," +
(dest.getLongitudeE6()/1.0E6);
//Walk attribute (for walk path)
strUrl += "&dirflg=w";
//File format
strUrl += "&output=kml";
try {
//Parse KML
URL url = new URL(strUrl.toString());
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser parser = saxFactory.newSAXParser();
XMLReader reader = parser.getXMLReader();
KMLHandler kmlHandler = new KMLHandler();
reader.setContentHandler(kmlHandler);
InputSource inputSource = new InputSource(url.openStream());
reader.parse(inputSource);
String path = kmlHandler.getPathCoordinates();
//Draw path
if(path != null) {
RouteOverlay routeOverlay = new RouteOverlay();
String pairs[] = path.split(" ");
for (String pair : pairs) {
String coordinates[] = pair.split(",");
GeoPoint geoPoint = new GeoPoint(
(int) (Double.parseDouble(coordinates[1]) * 1E6),
(int) (Double.parseDouble(coordinates[0]) * 1E6));
routeOverlay.addGeoPoint(geoPoint);
}
mapView.getOverlays().add(routeOverlay);
}
} catch (Exception e) {
Log.w("RoutePath", e.toString());
}
}
}
KML处理程序:
public class KMLHandler extends DefaultHandler {
private boolean inGeometryCollection = false;
private boolean inCoordinates = false;
private String pathCoordinates;
public String getPathCoordinates() { return pathCoordinates; }
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(inGeometryCollection && inCoordinates)
pathCoordinates = new String(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(localName.equals("GeometryCollection")) inGeometryCollection = false;
else if (localName.equals("coordinates")) inCoordinates = false;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equals("GeometryCollection")) inGeometryCollection = true;
else if (localName.equals("coordinates")) inCoordinates = true;
}
}
路线覆盖:
public class RouteOverlay extends Overlay {
private ArrayList<GeoPoint> geoPoints;
private int mRadius = 5;
public RouteOverlay() {
geoPoints = new ArrayList<GeoPoint>();
}
public void addGeoPoint(GeoPoint gp) {
geoPoints.add(gp);
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
if (shadow == false) {
Projection projection = mapView.getProjection();
Paint paint = new Paint();
paint.setAntiAlias(true);
GeoPoint geoPointFrom = null;
GeoPoint geoPointTo = null;
Point pointFrom = new Point();
Point pointTo = new Point();
//Route
for (GeoPoint geoPoint : geoPoints) {
if(geoPointFrom != null) {
geoPointTo = geoPoint;
projection.toPixels(geoPointFrom, pointFrom);
projection.toPixels(geoPointTo, pointTo);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(pointFrom.x, pointFrom.y,
pointTo.x, pointTo.y,
paint);
geoPointFrom = geoPointTo;
} else {
geoPointFrom = geoPoint;
}
}
//Start point
paint.setColor(Color.BLUE);
projection.toPixels(geoPoints.get(0), pointFrom);
RectF ovalStart = new RectF(pointFrom.x - mRadius,
pointFrom.y - mRadius,
pointFrom.x + mRadius,
pointFrom.y + mRadius);
canvas.drawOval(ovalStart, paint);
//Stop point
paint.setColor(Color.RED);
RectF ovalStop = new RectF(pointTo.x - mRadius,
pointTo.y - mRadius,
pointTo.x + mRadius,
pointTo.y + mRadius);
canvas.drawOval(ovalStop, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
}