답변

0

이 코드를 사용하면 도움이되기를 바랍니다 !!!

은 이제 맵에서 A부터 B까지 지정된 경로를 완전히 강조 표시합니다 (아래 스크린 샷). Highlight a specified route

public class PolyMap extends Activity { 
     ProgressDialog pDialog; 
     GoogleMap map; 
     List<LatLng> polyz; 
     JSONArray array; 
     static final LatLng DUBLIN = new LatLng(53.344103999999990000, 
       -6.267493699999932000); 

     @SuppressLint("NewApi") 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.map_layout); 
      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(DUBLIN, 15)); 
      map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
      new GetDirection().execute(); 
     } 

     class GetDirection extends AsyncTask<String, String, String> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(PolyMap.this); 
       pDialog.setMessage("Loading route. Please wait..."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      protected String doInBackground(String... args) { 
       Intent i = getIntent(); 
       String startLocation = i.getStringExtra("startLoc"); 
       String endLocation = i.getStringExtra("endLoc"); 
          startLocation = startLocation.replace(" ", "+"); 
        endLocation = endLocation.replace(" ", "+");; 
       String stringUrl = "http://maps.googleapis.com/maps/api/directions/json?origin=" + startLocation + ",+dublin&destination=" + endLocation + ",+dublin&sensor=false"; 
       StringBuilder response = new StringBuilder(); 
       try { 
        URL url = new URL(stringUrl); 
        HttpURLConnection httpconn = (HttpURLConnection) url 
          .openConnection(); 
        if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
         BufferedReader input = new BufferedReader(
           new InputStreamReader(httpconn.getInputStream()), 
           8192); 
         String strLine = null; 

         while ((strLine = input.readLine()) != null) { 
          response.append(strLine); 
         } 
         input.close(); 
        } 

        String jsonOutput = response.toString(); 

        JSONObject jsonObject = new JSONObject(jsonOutput); 

        // routesArray contains ALL routes 
        JSONArray routesArray = jsonObject.getJSONArray("routes"); 
        // Grab the first route 
        JSONObject route = routesArray.getJSONObject(0); 

        JSONObject poly = route.getJSONObject("overview_polyline"); 
        String polyline = poly.getString("points"); 
        polyz = decodePoly(polyline); 

       } catch (Exception e) { 

       } 

       return null; 

      } 

      protected void onPostExecute(String file_url) { 

       for (int i = 0; i < polyz.size() - 1; i++) { 
        LatLng src = polyz.get(i); 
        LatLng dest = polyz.get(i + 1); 
        Polyline line = map.addPolyline(new PolylineOptions() 
          .add(new LatLng(src.latitude, src.longitude), 
            new LatLng(dest.latitude,    dest.longitude)) 
          .width(2).color(Color.RED).geodesic(true)); 

       } 
       pDialog.dismiss(); 

      } 
     } 

     /* Method to decode polyline points */ 
     private List<LatLng> decodePoly(String encoded) { 

      List<LatLng> poly = new ArrayList<LatLng>(); 
      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; 

       LatLng p = new LatLng((((double) lat/1E5)), 
         (((double) lng/1E5))); 
       poly.add(p); 
      } 

      return poly; 
     } 
    } 
+0

이 이 코드에 오류가 작동하지 않습니다 ... 내가 이미이 링크 http://stackoverflow.com/questions/14760723/를 보려면이 코드를 시도 전체 코드 ... 을 제공하십시오 highlight-a-specified-route-on-google-maps-v2-android/21823468 # 21823468 – Karthik

관련 문제