2012-10-18 54 views
0

我想要顯示兩個位置之間的轉彎路線。 我試過android:獲取兩個地理位置之間的路線轉彎路線

uri = "http://maps.google.com/maps?saddr=" 
       + "lat1" + "," 
       + "lon1" + "&daddr=" 
       + "lat2" + "," + "long2"; 
     Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
       Uri.parse(uri)); 

它給出了路線。 但我不想打開瀏覽器並顯示。相反,有一種方法可以獲取路線,以便它可以以我自己的佈局顯示。前爲文本視圖。

+0

可以使用GoogleMap的API和解析器響應發送HTTP請求。 –

+0

你能告訴哪個API – png

+0

[谷歌方向API](https://developers.google.com/maps/documentation/directions/) –

回答

0

看看GreatMaps。他們有檢索路線的邏輯。它基於C#,只是使用它們的邏輯並在Java中編寫等價物。

http://greatmaps.codeplex.com/

1

這些代碼我在畢業設計中寫道所以他們沒有優化。

  1. MapService類:我從谷歌地圖的分析器數據響應。

    public class MapService { 
    public static final String TAG = "[MapService]"; 
    public static final String GET_DIRECTION_URL = "http://maps.google.com/maps/api/directions/json?" + "origin=%f,%f" + "&destination=%f,%f" + "&sensor=true&language=en&units=metric"; 
    
    public static DirectionResult getDirectionResult(double latitude1, double longtitude1, double latitude2, double longtitude2) { 
        String url = String.format(GET_DIRECTION_URL, latitude1, longtitude1, latitude2, longtitude2); 
        Log.d("URL", url); 
        // parse direction 
        DirectionParser directionParser = new DirectionParser() { 
         @Override 
         public void onSuccess(JSONObject json) throws JSONException { 
          // process response status 
          String status = json.getString("status"); 
          if (!status.equals("OK")) { 
           String error = null; 
    
           if (status.equals("NOT_FOUND") || status.equals("ZERO_RESULTS")) { 
            error = Global.application.getString(R.string.errCantGetDirection); 
           } else { 
            error = Global.application.getString(R.string.errGetDirection); 
           } 
    
           result.instructions = new String[] { error }; 
           result.hasError = true; 
           return; 
          } 
    
          /* 
          * routes[] legs[] steps[] html_instructions 
          */ 
          JSONArray arrRoutes = json.getJSONArray("routes"); 
    
          // no routes found 
          if (arrRoutes.length() == 0) { 
           result.instructions = new String[] { Global.application.getString(R.string.errCantGetDirection) }; 
           result.hasError = true; 
           return; 
          } 
    
          JSONArray arrLegs = arrRoutes.getJSONObject(0).getJSONArray("legs"); 
          JSONObject firstLeg = arrLegs.getJSONObject(0); 
          JSONArray arrSteps = firstLeg.getJSONArray("steps"); 
          int len = arrSteps.length(); 
          result.instructions = new String[len]; 
          result.points = new LinkedList<GeoPoint>(); 
          JSONObject leg = null; 
    
          // get instructions 
          for (int i = 0; i < len; ++i) { 
           leg = arrSteps.getJSONObject(i); 
           // location = leg.getJSONObject("start_location"); 
           String encoded = leg.getJSONObject("polyline").getString("points"); 
           result.points.addAll(decodePoly(encoded)); 
    
           result.instructions[i] = Html.fromHtml(leg.getString("html_instructions")).toString(); 
           Log.d("html_instructions", "" + Html.fromHtml(leg.getString("html_instructions"))); 
           // result.points[i] = new GeoPoint(
           // (int) (location.getDouble("lat") * 1E6), 
           // (int) (location.getDouble("lng") * 1E6)); 
          } 
    
          // location = leg.getJSONObject("end_location"); 
          // result.points[len] = new GeoPoint(
          // (int) (location.getDouble("lat") * 1E6), 
          // (int) (location.getDouble("lng") * 1E6)); 
    
          // distance and duration info 
          JSONObject distance = firstLeg.getJSONObject("distance"); 
          if (distance != null) { 
           result.distance = distance.getString("text"); 
          } 
          JSONObject duration = firstLeg.getJSONObject("duration"); 
          if (duration != null) { 
           result.duration = duration.getString("text"); 
          } 
         } 
    
         @Override 
         public void onFailure(String message) { 
          String error = "Error"; 
    
          result.instructions = new String[] { error }; 
          result.hasError = true; 
         } 
        }; 
    
        // return direction result 
        RestClient.getData(url, directionParser); 
        return directionParser.result; 
    } 
    
    private static List<GeoPoint> decodePoly(String encoded) { 
        List<GeoPoint> poly = new ArrayList<GeoPoint>(); 
        int index = 0, len = encoded.length(); 
        int lat = 0, lng = 0; 
    
        while (index < len) { 
         int b, shift = 0, result = 0; 
         do { 
          b = encoded.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 = encoded.charAt(index++) - 63; 
          result |= (b & 0x1f) << shift; 
          shift += 5; 
         } while (b >= 0x20); 
         int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
         lng += dlng; 
    
         GeoPoint p = new GeoPoint((int) (((double) lat/1E5) * 1E6), (int) (((double) lng/1E5) * 1E6)); 
         poly.add(p); 
        } 
    
        return poly; 
    } 
    
        } 
    
  2. 方向解析器和方向,結果

    public class DirectionParser extends JSONParser { 
    public DirectionResult result = new DirectionResult(); 
    
    protected int jsonType = JSONParser.GOOGLE_DIRECTION_JSON; 
    
    public DirectionParser() { 
        jsonType = JSONParser.GOOGLE_DIRECTION_JSON; 
    } 
    
    @Override 
    public void onFailure(String message) { 
    } 
    
    @Override 
    public void onSuccess(JSONObject json) throws JSONException { 
    } 
    } 
    
    public class DirectionResult { 
    public String[] instructions; 
    public List<GeoPoint> points; 
    public String duration; 
    public String distance; 
    public boolean hasError = false; 
    } 
    
  3. 你想要的是DirectionResult的指令。你可以簡單的創建一個數組適配器字符串數組

    instructions = directionResult.instructions;// DirectionResult you got from MapService. 
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(DirectionListActivity.this, android.R.layout.simple_list_item_1, instructions); 
           listDirection.setAdapter(adapter1); 
    

更新:另一個解析器我發現Google-Maps-Directions-API-Java-Parser

相關問題