0

我正在我的论文中开发一个安卓谷歌地图,我可以问一些关于安卓驾驶方向的问题吗?

这是我的问题。

“当一个按钮被点击时,我如何在安卓谷歌地图中生成一个随机的行车路线,它会随机给出从起点到目的地的路径。”

谢谢!。

我通过这个问了谷歌地图。

    private void parsing(GeoPoint start, GeoPoint end) throws ClientProtocolException, IOException, JSONException, URISyntaxException{
    HttpClient httpclient = new DefaultHttpClient();
    StringBuilder urlstring = new StringBuilder();
    urlstring.append("https://maps.googleapis.com/maps/api/directions/json?origin=")
    .append(Double.toString((double)start.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)start.getLongitudeE6()/1E6)).append("&destination=")
    .append(Double.toString((double)end.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)end.getLongitudeE6()/1E6))
    .append("&sensor=false");
    //urlstring.append("http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=true");
    url = new URI(urlstring.toString());

    HttpPost httppost = new HttpPost(url);

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = null;
    is = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    sb.append(reader.readLine() + "\n");
    String line = "0";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    reader.close();
    String result = sb.toString();
    JSONObject jsonObject = new JSONObject(result);
    JSONArray routeArray = jsonObject.getJSONArray("routes");
    JSONObject routes = routeArray.getJSONObject(0);
    JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
    String encodedString = overviewPolylines.getString("points");
    List<GeoPoint> pointToDraw = decodePoly(encodedString);

    //Added line:
    mv_mapview.getOverlays().add(new RoutePathOverlay(pointToDraw));
}

点在地图上。

    class MapOverlay extends Overlay {

        @Override
        public boolean onTap(final GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub

            geop_Tpoint = p;
            mcon_mapcontrol = mapView.getController();
            mcon_mapcontrol.animateTo(p);

            mv_mapview.invalidate();

            longitude = (int)(p.getLongitudeE6()*1E6);
            latitude = (int)(p.getLatitudeE6()*1E6);



            new AlertDialog.Builder(ShowMap.this)
                    .setTitle("Routes")
                    .setMessage("Display Routes?")
                    .setNegativeButton("NO",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub

                                    textlocation.setText("Tap in the Map for Destination!");
                                    dialog.dismiss();


                                }
                            })
                    .setPositiveButton("YES",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    try{
                                        textlocation.setText("");
                                        parsing(geop_startpoint,geop_Tpoint);
                                        geocodeExecuter(geop_startpoint,geop_Tpoint);
                                        showButton.setEnabled(true);


                                    }catch(Exception e){

                                        textlocation.setText(""+ e.getMessage());

                                    }


                                }

                            }).show();
            showButton.setEnabled(true);
            return true;
        }

起点是通过 GPS,它会告诉我我在哪里。

4

3 回答 3

5

这将帮助您获得从 A 点到 B 点的路线。

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=" + startLatitude + "," + startLongitude + "&daddr=" + destlat + "," + destlon));
startActivity(intent);
于 2013-01-16T07:14:55.103 回答
0

这取决于你如何询问谷歌地图的方向。如果您使用指定源和目标的链接创建意图,您可以尝试调用如下内容:

https://maps.google.com/maps?saddr=START_POINT&daddr=MIDDLE_POINT+to:DESTINATION_POINT

这也接受 GPS 坐标,所以你也可以这样调用:

https://maps.google.com/maps?saddr=START_POINT&daddr=XX.YYYYY,WW.ZZZZZ+to:DESTINATION_POINT

如果起点和终点是固定的,您可以尝试执行以下操作:

  • 获取起点的 GPS 坐标
  • 获取目的地的 GPS 坐标
  • 生成几个不超过源和目标定义的矩形的坐标(XY)

这应该在 A 和 B 之间的某处生成一个随机点。

如果您将中间点放在玉米地或类似的地方,谷歌地图足够聪明,可以选择最靠近道路的点来生成方向。


更新:阅读代码后:我认为获得结果的最快方法是找到 A 和 B 的 GPS 位置,然后在两者之间生成一个随机点并进行 2 次调用:A-RandomPoint B-RandomPoint。

总距离将是 AR 行程和 RB 行程之和,时间相同。

关于中点的计算: 第一:设置边界。如果你想计算 X:取 A 点的 X 和 B 点的 X。此时:minX 将是最低的,maxX 将是最高的(如果你的点很近,你可能要仔细考虑你在做什么绿女巫)。现在在你的最大值和最小值之间生成一个随机浮点数。这是一项微不足道的任务,并且有很多结果。只是一个例子:如何在 Java 中生成特定范围内的随机整数?

于 2012-11-21T13:47:57.500 回答
0

这就是我在Java中转换它的方式。

    public List<GeoPoint> findCoordinatesInACircle(double lat, double longi,double radius){



        double newLat;
        double newLong;

        List<GeoPoint> searchpoints = new ArrayList<GeoPoint>();


        double rLat=(radius/6371)*(180/Math.PI);  
        double rLon= rLat/Math.cos(longi * (Math.PI/180));

        for(int i =0 ; i < 181; i++ ){

            double iRad = i*(Math.PI/180);
            newLat = lat + (rLat * Math.sin(iRad));
            newLong = longi + (rLon * Math.cos(iRad));

            GeoPoint onePoint = new GeoPoint((int)(float)(newLat),(int)(float)(newLong));

            if(i%pointInterval == 0){
                searchpoints.add(onePoint); 
            }

        }
        return searchpoints;

    }
于 2012-12-04T11:27:04.583 回答