2016-09-12 6 views
8

मैं एंड्रॉइड के लिए MapsV2 में एक पॉलीलाइन के चारों ओर एक बफर डालने की कोशिश कर रहा हूं लेकिन मुझे यह नहीं पता कि अभी तक इसे कैसे किया जाए, मैं पॉलीलाइन के चारों ओर एक बहुभुज ड्राइंग करने पर सोच रहा था लेकिन ऐसा कोई भी नहीं मिला जिसने ऐसा किया है, क्या यह संभव है?एंड्रॉइड में पॉलीलाइन कैसे बफर करें या पॉलीलाइन के चारों ओर बहुभुज खींचें?

+0

के लिए कनेक्शन अपने एक नमूना छवि या एक स्केच कि आप उम्मीद कर रहे हैं प्रदान कर सकते हैं के लिए एक समारोह और उपयोग रेट्रोफिट बनाया? –

+0

हाय, Google मानचित्र API के बारे में 100% निश्चित नहीं है लेकिन नक्शे के लिए अन्य शक्तिशाली एपीआई हैं जो आपको खेलने के लिए पथ (* या पॉलीलाइन *) ऑब्जेक्ट के पॉइंटर की अनुमति दे सकती हैं। ** leaflet ** और ** mapbox.js पर एक नज़र डालें ** – SACn

उत्तर

0

ऐसा करने का सबसे अच्छा तरीका विधि mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(yourBounds,200)); विधि का उपयोग करना होगा। दूसरा तर्क (200) yourBounds में परिभाषित पॉली लाइन के चारों ओर पैडिंग है।

private GoogleMap mMap; 

public void addPolyline(ArrayList<LatLng> result) { 
    // Paint Lines on map 
    PolylineOptions lineOptions = null; 
    lineOptions = new PolylineOptions(); 
    lineOptions.addAll(result); 
    lineOptions.width(10); 
    lineOptions.color(Color.RED); 
    mMap.addPolyline(lineOptions); 
    LatLngBounds.Builder b = new LatLngBounds.Builder(); 
    for (LatLng l: result) { b.include(l); } 

    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(b.build(),200)); 
} 
+0

मुझे आज़माएं और आपको वापस आएं। –

+0

नहीं यह काम नहीं कर रहा है ... –

0

आप नक्शे पर पथ आकर्षित करने के लिए चाहते हैं या आप मानचित्र पर सीधी रेखाएं आकर्षित करने के लिए करना चाहते हैं कार्य करें:

यहाँ कुछ उदाहरण कोड है।

दोनों ही मामलों में आप नीचे दिए गए कोड का पालन कर सकते

PolylineOptions polyLineOptions = new PolylineOptions(); 
ArrayList<LatLng> points = new ArrayList<>(); 

//Code to populate Latlng 

polyLineOptions.addAll(points); 
polyLineOptions.width(mActivity.getResources().getDimensionPixelSize(R.dimen._2sdp)); 
polyLineOptions.color(ContextCompat.getColor(mContext, Color_Resource_id)); 
if (polyLineOptions != null) { 
    googleMap.addPolyline(polyLineOptions); 
} 

कोड ऊपर का नक्शा पर केवल सीधी रेखाएं आकर्षित करने के लिए है।

अब अगर आप मानचित्र पर पूर्ण पथ आकर्षित करना चाहते हैं, आप शायद गूगल दिशा एपीआई का उपयोग करने https://maps.googleapis.com/maps/api/directions/

की आवश्यकता होगी आप एक ही से प्रतिसाद पार्स करने में वर्ग नीचे का उपयोग कर सकते कुल latlngs

public class PathJSONParser { 

    public List<List<HashMap<String, String>>> parse(JSONObject jObject) { 
     List<List<HashMap<String, String>>> routes = new ArrayList<>(); 
     JSONArray jRoutes; 
     JSONArray jLegs; 
     JSONArray jSteps; 
     try { 
      jRoutes = jObject.getJSONArray("routes"); 
      /** Traversing all routes */ 
      for (int i = 0; i < jRoutes.length(); i++) { 
       jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs"); 
       List<HashMap<String, String>> path = new ArrayList<>(); 

       /** Traversing all legs */ 
       for (int j = 0; j < jLegs.length(); j++) { 
        jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps"); 

        /** Traversing all steps */ 
        for (int k = 0; k < jSteps.length(); k++) { 
         String polyline; 
         polyline = (String) ((JSONObject) ((JSONObject) jSteps 
           .get(k)).get("polyline")).get("points"); 
         List<LatLng> list = decodePoly(polyline); 

         /** Traversing all points */ 
         for (int l = 0; l < list.size(); l++) { 
          HashMap<String, String> hm = new HashMap<>(); 
          hm.put("lat", 
            Double.toString((list.get(l)).latitude)); 
          hm.put("lng", 
            Double.toString((list.get(l)).longitude)); 
          path.add(hm); 
         } 
        } 
        routes.add(path); 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return routes; 
    } 

    /** 
    * Method Courtesy : 
    * jeffreysambells.com/2010/05/27 
    * /decoding-polylines-from-google-maps-direction-api-with-java 
    * */ 
    private List<LatLng> decodePoly(String encoded) { 

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

पाने के लिए और अंत में आप इस अपनी कक्षा में साथ कोड

private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { 

     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 

      JSONObject jObject; 
      List<List<HashMap<String, String>>> routes = new ArrayList<>(); 

      try { 
       jObject = new JSONObject(jsonData[0]); 
       PathJSONParser parser = new PathJSONParser(); 
       routes = parser.parse(jObject); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return routes; 
     } 

     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> routes) { 
      if (routes != null && routes.size() > 0) { 
       new DrawPoly().execute(routes); 
      } 

     } 
    } 

    private class DrawPoly extends AsyncTask<List<List<HashMap<String, String>>>, 
      PolylineOptions, PolylineOptions> { 

     @Override 
     protected PolylineOptions doInBackground(List<List<HashMap<String, String>>>... params) { 
      PolylineOptions polyLineOptions = new PolylineOptions(); 
      ArrayList<LatLng> points; 

      if (params[0] != null && params[0].size() > 0) { 
       // traversing through routes 
       for (int j = 0; j < params[0].size(); j++) { 
        points = new ArrayList<>(); 
        polyLineOptions = new PolylineOptions(); 
        try { 
         List<HashMap<String, String>> path = params[0].get(j); 
         for (int k = 0; k < path.size(); k++) { 
          HashMap<String, String> point = path.get(k); 
          double lat = Double.parseDouble(point.get("lat")); 
          double lng = Double.parseDouble(point.get("lng")); 
          LatLng position = new LatLng(lat, lng); 
          points.add(position); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        polyLineOptions.addAll(points); 
        polyLineOptions.width(mActivity.getResources().getDimensionPixelSize(R.dimen.five)); 
        polyLineOptions.color(ContextCompat.getColor(mContext, R.color.red)); 
       } 
      } 
      return polyLineOptions; 
     } 

     @Override 
     protected void onPostExecute(PolylineOptions polyLineOptions) { 
      super.onPostExecute(polyLineOptions); 
      if (polyLineOptions != null) { 
       googleMap.addPolyline(polyLineOptions); 
      } 
     } 
    } 

नीचे का उपयोग कर सकते आप सुनिश्चित करें कि आप asyncTask में यह लगा रहे हैं बनाने की जरूरत है। मुझे उम्मीद है कि यह आपके लिए काम करेगा।

+0

प्लस 1 लिंक के लिए। – Wizard

+1

दोस्त पहले प्रश्नों को ध्यान से पढ़ें सबसे खराब जवाब कभी ... –

0

मुझे मानचित्र में पोलिलाइन जोड़ने का एक तरीका मिला है।

उपरोक्त लिंक से आपको Google मानचित्र एंड्रॉइड उपयोग से तीन कक्षाओं की आवश्यकता है। MathUtil, पॉलीयूटल उत्तर spheriacalUtil।

मैं गूगल

public void drawMapPath (String origin, String destination, String travel){ 

    RestInterface restInterface = ApiClient.getClient().create(RestInterface.class); 
    Call<GoogleDirections> call = restInterface.getDirections(origin, destination, travel, AppConfig.API_GOOGLEMAPS); 
    call.enqueue(new Callback<GoogleDirections>(){ 
     @Override 
     public void onResponse(Call<GoogleDirections> call, Response<GoogleDirections> response){ 
      if (response.isSuccessful()){ 
       Routes[] route = response.body().getRoutes(); 
       /** get bounds to set map after completion */ 
       LatLngBounds.Builder mapBounds = new LatLngBounds.Builder(); 
       LatLng northEast = new LatLng(Double.parseDouble(route[0].getBounds().getNortheast().getLat()),Double.parseDouble(route[0].getBounds().getNortheast().getLng())); 
       mapBounds.include(northEast); 
       LatLng southWest = new LatLng(Double.parseDouble(route[0].getBounds().getSouthwest().getLat()),Double.parseDouble(route[0].getBounds().getSouthwest().getLng())); 
       mapBounds.include(southWest); 
       /** no alternatives set so only one route*/ 
       Legs[] leg = route[0].getLegs(); 
       /** get the steps */ 
       Steps[] step =leg[0].getSteps(); 
       /** init points and options */ 
       points = new ArrayList(); 
       lineOptions = new PolylineOptions(); 
       /** Traversing through the steps */ 
       for (int i=0; i < step.length; i++){ 
        String polyline; 
        polyline = step[i].getPolyline().getPoints(); 
        /** classes needed from google */ 
        List<LatLng> list = decodePoly(polyline); 
        /** Traversing all points in list*/ 
        for (int n = 0; n < list.size(); n++) { 
         double lat = list.get(n).latitude; 
         double lon = list.get(n).longitude; 
         LatLng position=new LatLng(lat, lon); 
         points.add(position); 
        } 
       } 
       lineOptions.addAll(points); 
       lineOptions.width(12); 
       lineOptions.color(Color.BLUE); 
       lineOptions.geodesic(true); 
       googleMap.addPolyline(lineOptions); 
       googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(),10)); 
      } else { 
       Toast.makeText(getActivity(), getString(R.string.server_broken), Toast.LENGTH_SHORT).show(); 
      } 

     } 

     @Override 
     public void onFailure(Call<GoogleDirections> call, Throwable t) { 
      Log.d("Error ", t.getMessage()); 

     } 
    }); 
} 

private List<LatLng> decodePoly(String encoded) { 

    List<LatLng> poly = new ArrayList<>(); 
    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; 
} 
संबंधित मुद्दे