2012-10-18 1 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

을 발견했다. –

+0

당신은 어느 api에 문의 할 수 있습니까 – png

+0

[Google Directions API] (https://developers.google.com/maps/documentation/directions/) –

답변

0

그레이트 맵스를 확인하십시오. 그들은 경로를 검색하는 논리를 가지고 있습니다. 자사의 C#을 기반으로, 그냥 자신의 논리를 사용하고 자바에서 동급을 작성합니다.

http://greatmaps.codeplex.com/

1

그들이 최적화하지 않는, 그래서 난 내 졸업 프로젝트에 쓴이 코드입니다.

  1. MapService 클래스 : Google지도의 파서 데이터 응답.

    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); 
    

업데이트를 배열 어댑터를 만들 수 있습니다 : 다른 파서 나는 당신이 GoogleMap으로 API를 파서 응답을 사용하여 HTTP 요청을 보낼 수 있습니다 Google-Maps-Directions-API-Java-Parser