Android应用程序,显示点与当前位置之间的路径

时间:2013-05-17 17:47:45

标签: android google-maps

我正在尝试创建一个Android应用程序,它接收原点和目的地并沿着GPS线工作。

我想知道是否有可能(通过Google API)显示一个显示A点和B点之间路径的地图,但也会使用GPS在该路径上显示您当前的位置。我已经看过有关如何使用Google方向和地图API显示两点之间的路径但没有将其与当前位置点组合的教程和文章。

我还没有真正开始这个项目,因为我正在努力弄清楚如何最好地解决这个问题。任何帮助,教程,示例,建议将不胜感激!

2 个答案:

答案 0 :(得分:1)

您必须先获得当前的经度和纬度。您可以在以下链接中查看相同链接

http://developer.android.com/guide/topics/location/strategies.html

您必须获取源和目标之间的纬度和经度。应该使用asynctask来完成。

new connectAsyncTask().execute()   

asynctask类

 private class connectAsyncTask extends AsyncTask<Void, Void, Void>{
     private ProgressDialog progressDialog;
     @Override
     protected void onPreExecute() {
         // TODO Auto-generated method stub
         super.onPreExecute();
         progressDialog = new ProgressDialog(MainActivity.this);
         progressDialog.setMessage("Fetching route, Please wait...");
         progressDialog.setIndeterminate(true);
         progressDialog.show();
     }
     @Override
     protected Void doInBackground(Void... params) {
         // TODO Auto-generated method stub
         fetchData();
         return null;
     }
     @Override
     protected void onPostExecute(Void result) {
         super.onPostExecute(result);           
         if(doc!=null){
             NodeList _nodelist = doc.getElementsByTagName("status");
             Node node1 = _nodelist.item(0);
             String _status1  = node1.getChildNodes().item(0).getNodeValue();
             if(_status1.equalsIgnoreCase("OK")){
              Toast.makeText(MainActivity.this,"OK" , 1000).show();
                 NodeList _nodelist_path = doc.getElementsByTagName("overview_polyline");
                 Node node_path = _nodelist_path.item(0);
                 Element _status_path = (Element)node_path;
                 NodeList _nodelist_destination_path = _status_path.getElementsByTagName("points");
                 Node _nodelist_dest = _nodelist_destination_path.item(0);
                 String _path  = _nodelist_dest.getChildNodes().item(0).getNodeValue();
                 List<LatLng> points = decodePoly(_path);
                 for (int i = 0; i < points.size() - 1; i++) {
                   LatLng src = points.get(i);
                   LatLng dest = points.get(i + 1);
                    // Polyline to display the routes
                   Polyline line = mMap.addPolyline(new PolylineOptions()
                           .add(new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude,dest.longitude))
                           .width(2).color(Color.BLUE).geodesic(true))   
               }
                 progressDialog.dismiss();
             }else{
                               // Unable to find route
                  }
         }else{
                         // Unable to find route
         }
     }
 }

DecodePoly函数

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

获取数据

这里flati和flongi是源纬度和经度

dlati和dlongi是目的地纬度和经度

 Document doc = null;
 private void fetchData()
 {
     StringBuilder urlString = new StringBuilder();
     urlString.append("http://maps.google.com/maps/api/directions/xml?origin=");
     urlString.append( Double.toString(flati));
     urlString.append(",");
     urlString.append( Double.toString(flongi));
     urlString.append("&destination=");//to
     urlString.append( Double.toString(dlati));
     urlString.append(",");
     urlString.append( Double.toString(dlongi));
     urlString.append("&sensor=true&mode=walking");    
     Log.d("url","::"+urlString.toString());
     HttpURLConnection urlConnection= null;
     URL url = null;
     try
     {
         url = new URL(urlString.toString());
         urlConnection=(HttpURLConnection)url.openConnection();
         urlConnection.setRequestMethod("GET");
         urlConnection.setDoOutput(true);
         urlConnection.setDoInput(true);
         urlConnection.connect();
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         doc = (Document) db.parse(urlConnection.getInputStream());//Util.XMLfromString(response);
     }catch (MalformedURLException e){
         e.printStackTrace();
     }catch (IOException e){
         e.printStackTrace();
     }catch (ParserConfigurationException e){
         e.printStackTrace();
     }
     catch (SAXException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }

enter image description here

答案 1 :(得分:0)

您可以阅读我在此主题上撰写的指南,了解如何在您的应用中实施Google Map API V2:

Google Map API V2

然后你可以使用我在这里给出的答案来实现驾驶导航根目录:

Draw driving route between 2 GeoPoints on GoogleMap SupportMapFragment

要找到你当前的位置,你应该实现一个loicationListener,你可以在这里看到一个例子:

http://about-android.blogspot.co.il/2010/04/find-current-location-in-android-gps.html