2016-08-16 13 views
0

を通過した後、JSONからウェイポイントを抽出し、「アンドロイド」しかし、私は、アプリケーションの「ウェイポイント」を追加することによって、それを編集したい、2アドレスポイントを取り、地図上に表示されます。私の質問は、どのようにjsonからウェイポイント情報を抽出するのか、以下に抽出される開始情報と終了情報のようなものです。 StringJSONを変換するにはGoogleマップV2:以下のコード文字列配列

public class DirectionJsonParser { 
    private static final String DIRECTION_URL_API = "https://maps.googleapis.com/maps/api/directions/json?"; 
    private static final String GOOGLE_API_KEY = "key"; 
    private DirectionJsonListener listener; 
    private String origin; 
    private String destination; 

    public DirectionJsonParser(DirectionJsonListener listener, String origin, String destination) { 
     this.listener = listener; 
     this.origin = origin; 
     this.destination = destination; 
    } 

    public void execute() throws UnsupportedEncodingException { 
     listener.onDirectionFinderStart(); 
     new DownloadRawData().execute(createUrl()); 
    } 

    private String createUrl() throws UnsupportedEncodingException { 
     String urlOrigin = URLEncoder.encode(origin, "utf-8"); 
     String urlDestination = URLEncoder.encode(destination, "utf-8"); 

     return DIRECTION_URL_API + "origin=" + urlOrigin + "&destination=" + urlDestination + "&key=" + GOOGLE_API_KEY; 
    } 

    private class DownloadRawData extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      String link = params[0]; 
      try { 
       URL url = new URL(link); 
       InputStream is = url.openConnection().getInputStream(); 
       StringBuffer buffer = new StringBuffer(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

       String line; 
       while ((line = reader.readLine()) != null) { 
        buffer.append(line + "\n"); 
       } 

       return buffer.toString(); 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String res) { 
      try { 
       parseJSon(res); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    private void parseJSon(String data) throws JSONException { 
     if (data == null) 
      return; 

     List<Route> routes = new ArrayList<Route>(); 
     JSONObject jsonData = new JSONObject(data); 
     JSONArray jsonRoutes = jsonData.getJSONArray("routes"); 
     for (int i = 0; i < jsonRoutes.length(); i++) { 
      JSONObject jsonRoute = jsonRoutes.getJSONObject(i); 
      Route route = new Route(); 

      JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline"); 
      JSONArray jsonLegs = jsonRoute.getJSONArray("legs"); 
      JSONObject jsonLeg = jsonLegs.getJSONObject(0); 
      JSONObject jsonDistance = jsonLeg.getJSONObject("distance"); 
      JSONObject jsonDuration = jsonLeg.getJSONObject("duration"); 
      JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location"); 
      JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location"); 

      route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value")); 
      route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value")); 
      route.endAddress = jsonLeg.getString("end_address"); 
      route.startAddress = jsonLeg.getString("start_address"); 
      route.startLocation = new LatLng(jsonStartLocation.getDouble("lat"), jsonStartLocation.getDouble("lng")); 
      route.endLocation = new LatLng(jsonEndLocation.getDouble("lat"), jsonEndLocation.getDouble("lng")); 
      route.points = decodePolyLine(overview_polylineJson.getString("points")); 

      routes.add(route); 
     } 

     listener.onDirectionFinderSuccess(routes); 
    } 

    private List<LatLng> decodePolyLine(final String poly) { 
     int len = poly.length(); 
     int index = 0; 
     List<LatLng> decoded = new ArrayList<LatLng>(); 
     int lat = 0; 
     int lng = 0; 

     while (index < len) { 
      int b; 
      int shift = 0; 
      int result = 0; 
      do { 
       b = poly.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lat += dlat; 

      shift = 0; 
      result = 0; 
      do { 
       b = poly.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lng += dlng; 

      decoded.add(new LatLng(
        lat/100000d, lng/100000d 
      )); 
     } 

     return decoded; 
    } 
} 

答えて

0

、あなたはJSONStringerを使用することができます。 toString() and toString()を実装します。ほとんどのアプリケーション開発者は、これらのメソッドを直接使用し、このAPIを無視する必要があります。

JSONObject object = ... 
String json = object.toString(); 

各ストリンガーを使用して1つのトップレベル値をエンコードすることができます。このクラスのインスタンスはスレッドセーフではありません。このクラスは非最終クラスですが、継承のために設計されたものではなく、サブクラス化しないでください。特に、オーバーライド可能なメソッドによる自己使用は指定されていません。

ストリンガーは整形式JSONストリングのみをエンコードします。特に:

  • ストリンガーは、正確に1つのトップレベルの配列またはオブジェクトを持たなければなりません。
  • 字句スコープは、バランスをとらなければならない:array()のすべての呼び出しは'endArray()'に一致するコールと(オブジェクトへのすべてのコールを有していなければならない))endObject(整合用コールを有していなければなりません。
  • 配列にキー(プロパティ名)を含めることはできません。
  • オブジェクトは、キー(プロパティ名)と値を代替する必要があります。
  • 値は、リテラル値の呼び出し、または配列またはオブジェクトのネストによって挿入されます。

不正なJSON文字列が返される呼び出しは、JSONExceptionで失敗します。 APIの一部の実装では、最大20レベルのネストがサポートされています。 20レベル以上のネスティングを作成しようとすると、JSONExceptionで失敗することがあります。

関連する問題