【问题标题】:Android app that shows path between points along with current location显示点之间的路径以及当前位置的 Android 应用程序
【发布时间】:2013-05-17 17:47:44
【问题描述】:

我正在尝试创建一个 Android 应用程序,它接受起点和终点,并按照 GPS 的方式工作。

我想知道是否有可能(通过 Google API)显示一张地图,该地图显示 A 点和 B 点之间的路径,但也可以使用 GPS 在该路径顶部显示您当前的位置。我看过有关如何使用 Google 方向和地图 API 仅显示两点之间的路径但不将其与您当前的位置点结合起来的教程和文章。

我还没有真正开始这个项目,因为我正试图找出如何最好地解决这个问题。任何帮助、教程、示例、建议将不胜感激!

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

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

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

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

    new connectAsyncTask().execute()   
    

    异步任务类

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

    【讨论】:

    • 你愿意和我一起聊天@Raghunandan 吗?
    • @Zack 我不能聊天。我被SO阻止了。但是你可以问 Emil Adz。我只能再评论 7 天
    • @Zack 了解更多详情,您可以在此处查看教程raghunandankavi.blogspot.in。但是它可能看起来不太好
    【解决方案2】:

    您可以通过阅读我为此主题撰写的指南了解如何在您的应用程序中实现 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

    【讨论】:

    • 你的博客里没有这个吗?
    • @Raghunandan,我的博客有什么吗?
    • 关于在地图上显示两点之间的路线的教程。我也做了一个博客,但看起来不太好。你的看起来不错。尤其是黑色背景。你在用哪个博客?
    • 这是 Wordpress,我从他们的收藏中挑选了一些免费主题。如果是兴趣,我可以检查它是哪一个。关于你的问题,不,我还没有时间写这个话题。这在我的计划中,但尚未完成。
    • 我写了这个博客raghunandankavi.blogspot.in 看起来很糟糕,我使用了谷歌的博客。你可以让我知道主题
    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多