尝试简单的地理编码测试

时间:2014-04-20 10:43:54

标签: java android osmdroid

我正在尝试使用osmdroid api进行简单的地理编码测试,但我总是遇到错误,但我试图从网址(nominatim)获得json响应。我在AsyncTask中执行它但似乎gps坐标仅在try / catch中可用,但是在其余代码中它们被设置为null,因此我得到一个nullpointer异常...我该如何解决这个问题?

我的最终目的是设法在地图上显示EditText中用户所写的2个地址之间的道路

这是我的代码:

class OsmRoadTask extends AsyncTask<String, String, Void> {

    MapView mapView;
    MapController mapController;
    Context context;
    GeoPoint geostartpoint = null;
    GeoPoint geoendpoint = null;
    ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
    Polyline roadOverlay;
    ProgressDialog pd = new ProgressDialog(OsmRoadTaskActivity.this);
    InputStream isdepart = null;
    InputStream isarrivee = null;
    String resultdepart = "";
    String resultarrivee = "";
     RoadManager roadmanager = null;
     Road road = null;


    public OsmRoadTask(Context ctx) {
        // TODO Auto-generated constructor stub
    }

    protected void onPreExecute() {
        pd.setMessage("Recherche d'itineraire en cours, merci de patienter");
        pd.show();
        pd.setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface arg0) {

                OsmRoadTask.this.cancel(true);
            }
        });

    }

    protected Void doInBackground(String... arg0) {

        String adresseDepart = OsmRoadTaskActivity.Estartpoint.getText()
                .toString();
        String adresseDepartFormate = adresseDepart.replaceAll(" ", "+");
        String urldepart = "http://nominatim.openstreetmap.org/search?q="
                + adresseDepartFormate + "&format=json&polygon_svg=1";

        try {

            HttpGet httpgetdepart = new HttpGet(urldepart);
            HttpClient httpclientdepart = new DefaultHttpClient();
            HttpResponse responsedepart = httpclientdepart
                    .execute(httpgetdepart);
            HttpEntity httpentitydepart = responsedepart.getEntity();

            isdepart = httpentitydepart.getContent();

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            StringBuilder sbdepart = new StringBuilder();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(isdepart, "iso-8859-1"), 8);
            String line = null;
            while ((line = reader.readLine()) != null) {

                sbdepart.append(line + "\n");
            }
            isdepart.close();
            resultdepart = sbdepart.toString();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            JSONArray jArray = new JSONArray(resultdepart);
            for (int i = 0; i < jArray.length(); i++) {

                JSONObject jobject = jArray.getJSONObject(i);
                 double latdepart = jobject.getDouble("lat");
                  double lngarrivee = jobject.getDouble("lon");
                geostartpoint = new GeoPoint(latdepart, lngarrivee);

                // i think geostartpoint is not null here...

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        points.add(geostartpoint); //but here it's null


        roadmanager = new OSRMRoadManager();

        return null;
    }

    protected void onPostExecute(Void result) {

        mapView = (MapView) findViewById(R.id.mapview);
        mapController = (MapController) mapView.getController();

        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setBuiltInZoomControls(false);
        mapView.setMultiTouchControls(true);
        mapView.getOverlays().add(roadOverlay);
        Marker startmarker = new Marker(mapView);
        Marker endmarker = new Marker(mapView);
        startmarker.setPosition(geostartpoint);
        startmarker.setAnchor(Marker.ANCHOR_BOTTOM, Marker.ANCHOR_CENTER);
        // endmarker.setPosition(geoendpoint);
        // endmarker.setAnchor(Marker.ANCHOR_BOTTOM, Marker.ANCHOR_CENTER);

        mapView.getOverlays().add(startmarker);
        // mapView.getOverlays().add(endmarker);
        mapController.setCenter(geostartpoint);
        mapController.animateTo(geostartpoint);
        mapController.setZoom(13);
        mapView.invalidate();

    }

}

日志显示

04-20 14:14:23.593: E/AndroidRuntime(564): FATAL EXCEPTION: main
04-20 14:14:23.593: E/AndroidRuntime(564): java.lang.NullPointerException
04-20 14:14:23.593: E/AndroidRuntime(564):  at            org.osmdroid.bonuspack.overlays.Marker.setPosition(Marker.java:123)
04-20 14:14:23.593: E/AndroidRuntime(564):  at toutel.testcarte.OsmRoadTaskActivity$OsmRoadTask.onPostExecute(OsmRoadTaskActivity.java:278)
04-20 14:14:23.593: E/AndroidRuntime(564):  at toutel.testcarte.OsmRoadTaskActivity$OsmRoadTask.onPostExecute(OsmRoadTaskActivity.java:1)
04-20 14:14:23.593: E/AndroidRuntime(564):  at android.os.AsyncTask.finish(AsyncTask.java:631)
04-20 14:14:23.593: E/AndroidRuntime(564):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-20 14:14:23.593: E/AndroidRuntime(564):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-20 14:14:23.593: E/AndroidRuntime(564):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 14:14:23.593: E/AndroidRuntime(564):  at android.os.Looper.loop(Looper.java:137)
04-20 14:14:23.593: E/AndroidRuntime(564):  at android.app.ActivityThread.main(ActivityThread.java:4921)
04-20 14:14:23.593: E/AndroidRuntime(564):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 14:14:23.593: E/AndroidRuntime(564):  at java.lang.reflect.Method.invoke(Method.java:511)
04-20 14:14:23.593: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-20 14:14:23.593: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-20 14:14:23.593: E/AndroidRuntime(564):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案