代码之家  ›  专栏  ›  技术社区  ›  Akshay Cj

谷歌距离矩阵未获取距离

  •  0
  • Akshay Cj  · 技术社区  · 8 年前

    我已经大致尝试从 Google Distance Matrix API ,但它没有显示距离。

    我的GPS位置不是 0,0 我确信的。

    字符串距离=getMatrix(纬度、经度);

    我的函数代码是:

    private String getMatrix(double lat , double lang){
      JSONObject jObj;
      String getdistance = "";
      String strUrl = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" 
                      + Double.toString(my_lat) + "," 
                      + Double.toString(my_lang) 
                      + "&destinations="+ Double.toString(lat) + "," 
                      + Double.toString(lang) 
                      + "&mode=walking&sensor=false";
    
      String data = "";
      InputStream reader = null;
      HttpURLConnection urlConnection = null;
      try{
        URL url = new URL(strUrl);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
    
        reader = urlConnection.getInputStream();
        int bRead = -1;
        byte[] buffer = new byte[1024];
        do {
          bRead = reader.read(buffer, 0, 1024);
          if (bRead == -1) {
            break;
          }
          data += new String(buffer, 0, bRead);
        } while (true);
        urlConnection.disconnect();
      } catch(Exception e) {
      } finally {
    
      }
    
      try {
        jObj = new JSONObject(data);
    
        JSONArray rowsArray = jObj.getJSONArray("rows");
        JSONObject rows = rowsArray.getJSONObject(0);
    
        JSONArray elementsArray = rows.getJSONArray("elements");
        JSONObject newDisTimeOb = elementsArray.getJSONObject(0);
    
        JSONObject distOb = newDisTimeOb.getJSONObject("distance");
        getdistance = distOb.optString("text").toString();
      } catch (JSONException e) {
        e.printStackTrace();
      }
    
      return getdistance;
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   ישו אוהב אותך Mahavir    8 年前

    首先,检查是否在代码中正确构建了url字符串:

    String strUrl = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" 
                      + Double.toString(my_lat) + "," 
                      + Double.toString(my_lang) 
                      + "&destinations="+ Double.toString(lat) + "," 
                      + Double.toString(lang) 
                      + "&mode=walking&sensor=false";
    
    // Line to check if url code is right
    Log.d("APP", "strUrl = " + strUrl);
    

    然后检查以下代码,看它是否真的生成了数据:

    reader = urlConnection.getInputStream();
    int bRead = -1;
    byte[] buffer = new byte[1024];
    do {
      bRead = reader.read(buffer, 0, 1024);
      if (bRead == -1) {
        break;
      }
      data += new String(buffer, 0, bRead);
    } while (true);
    

    我的建议是使用 BufferedReader 而是读取每个字节的响应字节。

    getdistance = distOb.optString("text").toString();
    

    getdistance = distOb.getString("text");
    

    因为这里不需要再次转换为字符串。

    我已经搜索过了 Client Libraries for Google Maps Web Services 并找到 DistanceMatrixApi.java