示例#1
0
  public ArrayList<LatLng> getDirection(Document doc) {
    NodeList nl1, nl2, nl3;
    ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
    nl1 = doc.getElementsByTagName("step");
    if (nl1.getLength() > 0) {
      for (int i = 0; i < nl1.getLength(); i++) {
        Node node1 = nl1.item(i);
        nl2 = node1.getChildNodes();

        Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
        nl3 = locationNode.getChildNodes();
        Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
        double lat = Double.parseDouble(latNode.getTextContent());
        Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
        double lng = Double.parseDouble(lngNode.getTextContent());
        listGeopoints.add(new LatLng(lat, lng));

        locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
        nl3 = locationNode.getChildNodes();
        latNode = nl3.item(getNodeIndex(nl3, "points"));
        // ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
        List<LatLng> arr = PolyUtil.decode(latNode.getTextContent());
        for (int j = 0; j < arr.size(); j++) {
          listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
        }

        locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
        nl3 = locationNode.getChildNodes();
        latNode = nl3.item(getNodeIndex(nl3, "lat"));
        lat = Double.parseDouble(latNode.getTextContent());
        lngNode = nl3.item(getNodeIndex(nl3, "lng"));
        lng = Double.parseDouble(lngNode.getTextContent());
        listGeopoints.add(new LatLng(lat, lng));
      }
    }

    return listGeopoints;
  }
示例#2
0
  public List<LatLng> getDirections(String pidStart, String pidEnd, String mode) {
    // public Document getDirections(LatLng start, LatLng end, String mode) {
    String strUrl =
        "https://maps.googleapis.com/maps/api/directions/json?"
            + "origin=place_id:"
            + pidStart // + start.latitude + "," + start.longitude
            + "&destination=place_id:"
            + pidEnd // end.latitude + "," + end.longitude
            + "&mode=DRIVING&key="
            + API_KEY;

    Log.d("url", strUrl);

    /*try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder;
        builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }*/

    ArrayList<LatLng> latLngs = new ArrayList<LatLng>();
    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {

      URL url = new URL(strUrl);

      conn = (HttpURLConnection) url.openConnection();
      InputStreamReader in = new InputStreamReader(conn.getInputStream());

      int read;
      char[] buff = new char[1024];
      while ((read = in.read(buff)) != -1) {
        jsonResults.append(buff, 0, read);
      }

      /* DocumentBuilder builder;
      builder = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder();
      Document doc = builder.parse(conn.getInputStream());
      return doc;*/

    } catch (MalformedURLException e) {
      Log.e(TAG, "Error processing Places API URL", e);
      return null;
    } catch (IOException e) {
      Log.e(TAG, "Error connecting to Places API", e);
      return null;
    } /*catch (ParserConfigurationException e) {
          e.printStackTrace();
      } catch (SAXException e) {
          e.printStackTrace();
      }*/ finally {
      if (conn != null) {
        conn.disconnect();
      }
    }

    try {
      JSONObject jsonObj = new JSONObject(jsonResults.toString());
      String jsonSteps =
          jsonObj
              .getJSONArray("routes")
              .getJSONObject(0)
              .getJSONObject("overview_polyline")
              .getString("points");
      /* .getJSONArray("legs")
      .getJSONObject(0)
      .getJSONArray("steps");*/

      /* float lat, lng;
      for(int i = 0; i < jsonSteps.length(); i++){
          lat = Float.parseFloat(jsonSteps.getJSONObject(i).getJSONObject("start_location").getString("lat"));
          lng = Float.parseFloat(jsonSteps.getJSONObject(i).getJSONObject("start_location").getString("lng"));
          latLngs.add(new LatLng(lat,lng));

          lat = Float.parseFloat(jsonSteps.getJSONObject(i).getJSONObject("end_location").getString("lat"));
          lng = Float.parseFloat(jsonSteps.getJSONObject(i).getJSONObject("end_location").getString("lng"));

          latLngs.add(new LatLng(lat,lng));
      }

      return latLngs;*/

      Log.d("tons", jsonSteps);
      return PolyUtil.decode(jsonSteps); // decodePolyline(jsonSteps);

    } catch (JSONException e) {
      Log.e(TAG, "Cannot process JSON results", e);
    }

    return null;
  }