0

我一直在关注这个教程。

我在doc = (Document) urlConnection.getInputStream();.

这是我的代码:

public class DirectionActivity extends MapActivity {
    MapView myMapView = null;
    MapController myMC = null;
    GeoPoint geoPoint = null;
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document doc;
    HttpURLConnection urlConnection ;
    URL url ;
    String pathConent ;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myMapView = (MapView) findViewById(R.id.mapview1);
    geoPoint = null;
    myMapView.setSatellite(false);

    String pairs[] = getDirectionData("Ahmedabad", "Goa");
    String[] lngLat = pairs[0].split(",");

    // STARTING POINT
    GeoPoint startGP = new GeoPoint(
            (int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
                    .parseDouble(lngLat[0]) * 1E6));

    myMC = myMapView.getController();
    geoPoint = startGP;
    myMC.setCenter(geoPoint);
    myMC.setZoom(15);
    myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

    // NAVIGATE THE PATH
    GeoPoint gp1;
    GeoPoint gp2 = startGP;

    for (int i = 1; i < pairs.length; i++) {
        lngLat = pairs[i].split(",");
        gp1 = gp2;
        // watch out! For GeoPoint, first:latitude, second:longitude
        gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
                (int) (Double.parseDouble(lngLat[0]) * 1E6));
        myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
        Log.d("xxx", "pair:" + pairs[i]);
    }

    // END POINT
    myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));

    myMapView.getController().animateTo(startGP);
    myMapView.setBuiltInZoomControls(true);
    myMapView.displayZoomControls(true);

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

private String[] getDirectionData(String srcPlace, String destPlace) {

    String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
            + srcPlace + "&daddr=" + destPlace
            + "&ie=UTF8&0&om=0&output=kml";
    Log.d("URL", urlString);

    try {

        url = new URL(urlString.toString());
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();

        dbf = DocumentBuilderFactory.newInstance();
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ***doc = (Document) urlConnection.getInputStream();***(getting doc=null)


    } catch (Exception e) {
    }

    NodeList nl = doc.getElementsByTagName("LineString");
    for (int s = 0; s < nl.getLength(); s++) {
        Node rootNode = nl.item(s);
        NodeList configItems = rootNode.getChildNodes();
        for (int x = 0; x < configItems.getLength(); x++) {
            Node lineStringNode = configItems.item(x);
            NodeList path = lineStringNode.getChildNodes();
            pathConent = path.item(0).getNodeValue();
        }
    }
    String[] tempContent = pathConent.split(" ");
    return tempContent;
}

public class DirectionPathOverlay extends Overlay {
    private GeoPoint gp1;
    private GeoPoint gp2;
    public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
        this.gp1 = gp1;
        this.gp2 = gp2;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        // TODO Auto-generated method stub
        Projection projection = mapView.getProjection();
        if (shadow == false) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Point point = new Point();
            projection.toPixels(gp1, point);
            paint.setColor(Color.BLUE);
            Point point2 = new Point();
            projection.toPixels(gp2, point2);
            paint.setStrokeWidth(2);
            canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                    (float) point2.y, paint);
        }
        return super.draw(canvas, mapView, shadow, when);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub

        super.draw(canvas, mapView, shadow);
    }

}
}

这是Logcat:

01-03 10:56:33.447: E/AndroidRuntime(1018): FATAL EXCEPTION: main
01-03 10:56:33.447: E/AndroidRuntime(1018): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.itsmyway/com.example.itsmyway.DirectionActivity}: java.lang.NullPointerException
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.os.Looper.loop(Looper.java:137)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at dalvik.system.NativeStart.main(Native Method)
01-03 10:56:33.447: E/AndroidRuntime(1018): Caused by: java.lang.NullPointerException
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.example.itsmyway.DirectionActivity.getDirectionData(DirectionActivity.java:109)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.example.itsmyway.DirectionActivity.onCreate(DirectionActivity.java:41)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.Activity.performCreate(Activity.java:5008)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-03 10:56:33.447: E/AndroidRuntime(1018):     ... 11 more
4

1 回答 1

1

我已经实现了相同的示例并且面临同样的问题。

您的应用程序所做的是从 google 服务器检索文档,然后在该文档中查找说明。尽管 kml 文档(您的代码想要检索的文档)过去可以工作几个月(就像编写该教程时一样),但自从 2012 年 12 月 google 更新了他们的 API 以来,它现在无法工作。

解决方案:您的 URL 似乎已被弃用。谷歌地图使用 JSON 和 XML,而不是 kml。查看此页面以使用 Web 服务。

现在,

1) 根据这些 Web 服务的指南创建一个 URL。

2) 现在向谷歌服务器发送一个 HTTP 连接请求,你必须得到一个 Inputstream。


您的问题已经结束,但是如果您在解决此问题后使用相同的代码,您将遇到解析接收到的文档的另一个问题。

第二个问题的解决方案:

1)将文档保存在手机sdcard中,手动查看。存储文档的指南在这里

2) 根据页面上的 XML 输出部分在文档中找到方向。

3) 使用 DocumentBuilderFactory 或其他方式检索这些方向。

您的应用程序在 kml 文档中查找标记“LineString”。但是,由于现在您使用的是 XML 或 JSON,因此请查找指南中给出的相关标签。

于 2013-01-03T08:58:37.160 回答