2016-03-08 137 views
0

我想從SQLite的地圖上繪製我的Lat long位置的路徑。這是可能的,任何人都可以在Android上幫助我嗎?我附加了我的地圖活動的位置。從Lat/Long繪製路徑

+2

歡迎計算器!請閱讀www.stackoverflow.com/help/how-to-ask,瞭解如何提出適當的問題。 – SubliemeSiem

+0

普里揚卡什麼你想要做我不明白..你可以exaplain它。 –

+0

你只想繪製當前位置到目的地的路徑? –

回答

2

你可以試試這個

PolylineOptions rectOptions = new PolylineOptions() 
     .add(new LatLng(37.35, -122.0)) 
     .add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude 
     .add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west 
     .add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south 
     .add(new LatLng(37.35, -122.0)); // Closes the polyline. 

// Get back the mutable Polyline 
Polyline polyline = myMap.addPolyline(rectOptions); 

或數據庫

Cursor cursor = database,getData("your query"); 
PolylineOptions rectOptions = new PolylineOptions(); 

if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()) 
{ 

do 
{ 

String lt = cursor.getString(curor.getColumnIndex("columnName")); 
double lat = Double.parseDouble(lt); 
String ln = cursor.getString(curor.getColumnIndex("columnName")); 
double lng = Double.parseDouble(ln); 


     rectOptions.add(new LatLng(lat, lng)); // Closes the polyline. 

// Get back the mutable Polyline 



}while(cursor.moveToNext()); 

} 
Polyline polyline = myMap.addPolyline(rectOptions); 
+0

謝謝先生,現在我上面的數據庫代碼爲我工作,我想通過路線加入我的位置。你可以幫我加入我的路線 –

+0

是的,你可以使用googe api來獲得兩個地點之間的路線 –

+0

先生如何將它實現成代碼請幫助我 –

0
**Create Seperate class** 

class DirectionsJSONParser { 

    /** 
    * Receives a JSONObject and returns a list of lists containing latitude and longitude 
    */ 
    public List<List<HashMap<String, String>>> parse(JSONObject jObject) { 

     List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>(); 
     JSONArray jRoutes = null; 
     JSONArray jLegs = null; 
     JSONObject jDistance = null; 
     JSONArray jSteps = null; 

     try { 

      jRoutes = jObject.getJSONArray("routes"); 

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

       /** Traversing all legs */ 
       for (int j = 0; j < jLegs.length(); j++) { 
        /** Getting distance from the json data */ 
        jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance"); 
        HashMap<String, String> hmDistance = new HashMap<String, String>(); 
        hmDistance.put("distance", jDistance.getString("value")); 
        jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps"); 

        /** Adding distance object to the path */ 
        path.add(hmDistance); 

        /** 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<String, String>(); 
          hm.put("lat", Double.toString(((LatLng) list.get(l)).latitude)); 
          hm.put("lng", Double.toString(((LatLng) list.get(l)).longitude)); 
          path.add(hm); 
         } 
        } 
        routes.add(path); 
       } 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
     } 

     return routes; 
    } 

    /** 
    * Method to decode polyline points 
    * Courtesy : http://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<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; 
    } 
} 

- **In Activtiy** 


private String getDirectionsUrl(LatLng origin, LatLng dest) { 

     // Origin of route 
     String str_origin = "origin=" + origin.latitude + "," + origin.longitude; 

     // Destination of route 
     String str_dest = "destination=" + dest.latitude + "," + dest.longitude; 

     // Sensor enabled 
     String sensor = "sensor=false"; 

     // Building the parameters to the web service 
     String parameters = str_origin + "&" + str_dest + "&" + sensor; 

     // Output format 
     String output = "json"; 

     // Building the url to the web service 
     String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; 

     return url; 
    } 

    // Fetches data from url passed 
    private class DownloadTask extends AsyncTask<String, Void, String> { 

     // Downloading data in non-ui thread 
     @Override 
     protected String doInBackground(String... url1) { 
      // For storing data from web service 
      String data = ""; 
      try { 
       // Fetching the data from web service 
       InputStream iStream = null; 
       HttpURLConnection urlConnection = null; 
       try { 
        URL url = new URL(url1[0]); 

        // Creating an http connection to communicate with url 
        urlConnection = (HttpURLConnection) url.openConnection(); 
        // Connecting to url 
        urlConnection.connect(); 
        // Reading data from url 
        iStream = urlConnection.getInputStream(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 
        StringBuilder sb = new StringBuilder(); 
        String line = ""; 
        while ((line = br.readLine()) != null) { 
         sb.append(line); 
        } 
        data = sb.toString(); 
        br.close(); 
       } catch (Exception e) { 
        Log.d(TAG, e.toString()); 
       } finally { 
        iStream.close(); 
        urlConnection.disconnect(); 
       } 
      } catch (Exception e) { 
       Log.d(TAG, e.toString()); 
      } 
      return data; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      ParserTask parserTask = new ParserTask(); 
      // Invokes the thread for parsing the JSON data 
      parserTask.execute(result); 
     } 
    } 

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

     // Parsing the data in non-ui thread 
     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 

      JSONObject jObject; 
      List<List<HashMap<String, String>>> routes = null; 

      try { 
       jObject = new JSONObject(jsonData[0]); 
       DirectionsJSONParser parser = new DirectionsJSONParser(); 

       // Starts parsing data 
       routes = parser.parse(jObject); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return routes; 
     } 

     // Executes in UI thread, after the parsing process 
     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
      ArrayList<LatLng> points; 
      PolylineOptions lineOptions = null; 
      // Traversing through all the routes 
      for (int i = 0; i < result.size(); i++) { 
       points = new ArrayList<LatLng>(); 
       lineOptions = new PolylineOptions(); 

       // Fetching i-th route 
       List<HashMap<String, String>> path = result.get(i); 

       // Fetching all the points in i-th route 
       for (int j = 0; j < path.size(); j++) { 
        HashMap<String, String> point = path.get(j); 

        if (j == 0) { // Get distance from the list 

          String dist = point.get("distance"); 
         if(dist!=null && dist.length()>0) { 
          int distInt = Integer.parseInt(dist); 
          sumDistance += distInt; 
         } 
         continue; 
        } 
        double lat = Double.parseDouble(point.get("lat")); 
        double lng = Double.parseDouble(point.get("lng")); 
        LatLng position = new LatLng(lat, lng); 

        points.add(position); 
       } 

       // Adding all the points in the route to LineOptions 
       lineOptions.addAll(points); 
       lineOptions.width(10); 

        lineOptions.color(Color.BLUE); 

      } 
      mGoogleMap.addPolyline(lineOptions); 
      if (true) { 
       // Drawing polyline in the Google Map for the i-th route 
       mGoogleMap.addPolyline(lineOptions); 
      } 
      //Log.d(TAG, "map: " + map + " lineoptions: " + lineOptions); 
     } 
    } 

calling: 

    String url = getDirectionsUrl(position, position1); 
    DownloadTask downloadTask = new DownloadTask(); 
    // Start downloading json data from Google Directions API 
    downloadTask.execute(url); 
+0

我不明白上面的代碼添加latlng的位置和position1 ........下面是我的代碼plz告訴我怎麼可以做 –

+0

您好先生,我使用上面的代碼,它的工作在我的case.I對此的另一個問題是,我想顯示兩點之間只有一條路線.....在這裏它顯示了多條路線。你可以解決我的問題 –