我正在为Google地图上的路线做一个应用程序。但是当我运行应用程序时,我遇到了问题11-23 09:39:16.111: E/MapActivity(572): Couldn't get connection factory client
并且它没有在地图上显示/绘制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());
}
}
}
KMLHandler :
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;
}
}
RouteOverlay :
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);
}
}
答案 0 :(得分:0)
我认为你仍然需要ItemizedOverlay,因为这是MapActivity知道你有Overlay项目的方式。路线是叠加。请参阅此问题:Android draw route on a Mapview with twoo POI-s