创建行程谷歌地图安卓

Posted

技术标签:

【中文标题】创建行程谷歌地图安卓【英文标题】:create itinary google maps android 【发布时间】:2016-12-19 12:25:59 【问题描述】:

使用 gps 进行地理定位后,我想创建一条线来显示两点之间的行程。我知道我需要使用折线,但是在 JSON 解析之后,我不知道如何使用折线选项,也不知道如何使用 Json 中名为“steps”的部分。对于 Http 连接,我使用 Volley 库。这是我的代码:

public void geoLocate(View view)

        String start = start_address.getText().toString();
        String destination = destination_address.getText().toString();

        Geocoder gc = new Geocoder(getActivity());
        try 

            List<android.location.Address> list = gc.getFromLocationName(start,1);
            final Address adress1 =  list.get(0);
            String  start_adress = adress1.getLocality();
            double lat_start = adress1.getLatitude();
            double lng_start = adress1.getLongitude();


            list = gc.getFromLocationName(destination,1);
            Address adress2 =  list.get(0);
            String  destination_adress = adress2.getLocality();
            double lat_destination = adress2.getLatitude();
            double lng_destination = adress2.getLongitude();

            if (start_marker != null || destination_marker != null)
                start_marker.remove();
                destination_marker.remove();
            

            options_start = new MarkerOptions()
                    .title(start_adress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                    .position(new LatLng(lat_start, lng_start));
            start_marker = mGoogleMap.addMarker(options_start);
            options_destination = new MarkerOptions()
                    .title(destination_adress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                    .position(new LatLng(lat_destination, lng_destination));
            destination_marker = mGoogleMap.addMarker(options_destination);
            reservation.setClickable(true);
            reservation.setEnabled(true);
            reservation.setTextColor(getResources().getColor(R.color.white));
            reservation.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            if(reservation.isClickable()) 
                reservation.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                    
                );
            

            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            builder.include(start_marker.getPosition());
            builder.include(destination_marker.getPosition());
            LatLngBounds bounds = builder.build();
            final StringBuilder url = new StringBuilder("http://maps.googleapis.com/maps/api/directions/json?sensor=false&language=fr");
            url.append("&origin=");
            url.append(start.replace(" ", "+"));
            url.append("&destination=");
            url.append(destination.replace(" ", "+"));


            final Response.Listener<String> responseListener = new Response.Listener<String>() 
                @Override
                public void onResponse(final String response) 

                    try 
                        JSONObject jsonResponse = new JSONObject(response);
                        String status = jsonResponse.getString("status");

                        if (status.equals("OK"))
                            JSONArray parentArray = jsonResponse.getJSONArray("routes");
                                JSONObject routes  = parentArray.getJSONObject(0);
                                if(routes != null)

                                    JSONArray legsArray = routes.getJSONArray("legs");
                                    JSONObject legsObject = legsArray.getJSONObject(0);
                                    if (legsObject != null)

                                        JSONObject distance = legsObject.getJSONObject("distance");
                                        if (distance != null)
                                            String distanceText = distance.getString("text");
                                            distance_matrix.append(" "+distanceText);
                                        

                                        JSONArray stepsArray = legsObject.getJSONArray("steps");
                                        JSONObject stepsObject = stepsArray.getJSONObject(0);
                                        if (stepsObject != null)


                                        

                                    

                                
                        

                     catch (JSONException e) 
                        e.printStackTrace();
                    

                

            ;


            ItineraireRequest itineraireRequest = new ItineraireRequest(url.toString(), responseListener);
            RequestQueue queue = Volley.newRequestQueue(getActivity());
            queue.add(itineraireRequest);
            int padding = 125; // offset from edges of the map in pixels
            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
            mGoogleMap.animateCamera(cu);



         catch (IOException e) 
            e.printStackTrace();
        

    

请您提供一个适合我的代码的解决方案吗?

【问题讨论】:

This 应该会有所帮助 HttpClient 已弃用 【参考方案1】:

我认为这会有所帮助,“getDocument()”可以完成所有工作

@Override
public void onMapReady(GoogleMap googleMap) 
        GMapV2Direction md = new GMapV2Direction();
        Document doc = md.getDocument(formatAddress(ORIGIN_TEXT, formatAddress(DESTINATION_TEXT, MODE, getApplicationContext());
        if (doc != null) 

            int duration = md.getDurationValue(doc);

            if (duration > 0) 
                try 

                    ArrayList<LatLng> directionPoint = md.getDirection(doc);
                    PolylineOptions rectLine = new PolylineOptions().width(9).color(R.color.splash_blue).geodesic(true);

                    for (int i = 0; i < directionPoint.size(); i++) 
                        rectLine.add(directionPoint.get(i));
                    

                    AccessLocationActivity.this.mMap.addPolyline(rectLine);
                 catch (Exception e) 
                    e.printStackTrace();
                

        

更新

试试这个

public void geoLocate(View view)

        String start = start_address.getText().toString();
        String destination = destination_address.getText().toString();

        Geocoder gc = new Geocoder(getActivity());
        try 

            List<android.location.Address> list = gc.getFromLocationName(start,1);
            final Address adress1 =  list.get(0);
            String  start_adress = adress1.getLocality();
            double lat_start = adress1.getLatitude();
            double lng_start = adress1.getLongitude();


            list = gc.getFromLocationName(destination,1);
            Address adress2 =  list.get(0);
            String  destination_adress = adress2.getLocality();
            double lat_destination = adress2.getLatitude();
            double lng_destination = adress2.getLongitude();

            if (start_marker != null || destination_marker != null)
                start_marker.remove();
                destination_marker.remove();
            

            options_start = new MarkerOptions()
                    .title(start_adress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                    .position(new LatLng(lat_start, lng_start));
            start_marker = mGoogleMap.addMarker(options_start);
            options_destination = new MarkerOptions()
                    .title(destination_adress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                    .position(new LatLng(lat_destination, lng_destination));
            destination_marker = mGoogleMap.addMarker(options_destination);
            reservation.setClickable(true);
            reservation.setEnabled(true);
            reservation.setTextColor(getResources().getColor(R.color.white));
            reservation.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            if(reservation.isClickable()) 
                reservation.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                    
                );
            

            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            builder.include(start_marker.getPosition());
            builder.include(destination_marker.getPosition());
            LatLngBounds bounds = builder.build();
            final StringBuilder url = new StringBuilder("http://maps.googleapis.com/maps/api/directions/json?sensor=false&language=fr");
            url.append("&origin=");
            url.append(start.replace(" ", "+"));
            url.append("&destination=");
            url.append(destination.replace(" ", "+"));


            GMapV2Direction md = new GMapV2Direction();
            Document doc = md.getDocument(formatAddress(start), formatAddress(destination), GMapV2Direction.MODE_DRIVING, getApplicationContext());
            if (doc != null) 

                int duration = md.getDurationValue(doc);

                if (duration > 0) 
                    try 

                        ArrayList<LatLng> directionPoint = md.getDirection(doc);
                        PolylineOptions rectLine = new PolylineOptions().width(9).color(R.color.colorPrimary).geodesic(true);

                        for (int i = 0; i < directionPoint.size(); i++) 
                            rectLine.add(directionPoint.get(i));
                        

                        mGoogleMap.addPolyline(rectLine);
                     catch (Exception e) 
                        e.printStackTrace();
                    


         catch (IOException e) 
            e.printStackTrace();
        

    

【讨论】:

你可以给我这个,但请用我的脚本吗? 其实什么是GMapV2Direction.class?我没有任何名为 GMapV2Direction 的类 抱歉我迟到了你可以在这里导入该类github.com/KAPLANDROID/…【参考方案2】:

This 是 Google DirectionsAPI 之上的一个很好的包装器,如果您愿意试一试的话。

更详细的解释here。

【讨论】:

以上是关于创建行程谷歌地图安卓的主要内容,如果未能解决你的问题,请参考以下文章

谷歌方向 API 行程时间与实时谷歌地图不同

安卓谷歌地图空白

即使安卓设备离线,谷歌地图如何获取地址

为啥手机上不能用谷歌地图

如何使谷歌地图像安卓手机中的谷歌地图应用程序一样工作

谷歌距离矩阵与方向 API