Google地图路线来自JSON结果

时间:2016-09-29 04:51:45

标签: android json google-maps routes

所以我想从JSON数组的结果中添加不同颜色的路由(每条路由的颜色不同)。阵列看起来像这样,但显然要长得多:

[  
    {  
        "lat":44.245254209999999,
        "dist":0.0,
        "lng":2.0344993799999997,
        "speed":0.82999998331069946,
        "index":0
    },
    {  
        "lat":41.245254209999999,
        "dist":0.00051573936104906324,
        "lng":2.0344993799999997,
        "speed":0.82999998331069946,
        "index":100
    }
]

可在此处找到一个真实示例: http://pastebin.com/sNiZ8vbQ

我从这样的URL获取此信息:https://s3-us-west-2.amazonaws.com/chffrprivate/comma-25b94166c3ae208c/0bb18eb5e944350a4f52ce3fd4958669_2016-07-25--10-08-07/route.coords每条路由都有自己的URL / route.coords。在过去通过驱动器循环我做了这样的事情:

public class DriveURLAdapter extends ArrayAdapter<Drive> {

    public static String DriveURL;
    public static long driveSeconds;
    public static DateTime startTime;
    public static DateTime endTime;
    private final Activity context;
    // you may need to change List to something else (whatever is returned from drives.entrySet())
    private final List<Drive> drives;
    SharedPreferences sharedPref;
    // may also need to change List here (above comment)
    public DriveURLAdapter(Activity context, List<Drive> drives) {
        super(context, R.layout.drive_url_list, drives);
        this.context = context;
        this.drives = drives;
        sharedPref = context.getPreferences(Context.MODE_PRIVATE);
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.drive_url_list, null, true);

        Drive drive = this.drives.get(position);

        // position is the index of the drives.entrySet() array
        int driveNum = position + 1;

        // need to import your Route class
        Route route = drive.getRoute();

        // need to import your URL class
        DriveURL = route.getUrl();

        startTime = new DateTime(route.getStart_time());
        endTime = new DateTime(route.getEnd_time());
        driveSeconds = ((endTime.getMillis() - startTime.getMillis())/ 1000);
        System.out.println(driveSeconds);

        TextView spinnerItems = (TextView) rowView.findViewById(R.id.Spinner_Items);
        spinnerItems.setText(String.valueOf(driveNum));
        //driveURLList.setText(DriveURL);

        return rowView;
    }
}

但是,这有一个getView,我没有看到这种情况。到目前为止,我正在努力编写价值观。

public class ThirdFragment extends Fragment implements OnMapReadyCallback {
    View myView;
    private GoogleMap mMap;
    MapFragment mapFrag;
    private Polyline line;
    private PolylineOptions lineOptions;



    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.third_layout, container, false);
        return myView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapFrag = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);
        //mapFrag.onResume();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            Criteria criteria = new Criteria();
            LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            String provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            double lat =  location.getLatitude();
            double lng = location.getLongitude();
            LatLng coordinate = new LatLng(lat, lng);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13);
            mMap.animateCamera(yourLocation);
            lineOptions = new PolylineOptions()
                    .add(new LatLng(33.927085040000001, -118.39129683))
                    .add(new LatLng(33.927085969382027, -118.39129820011991))
                    .add(new LatLng(33.927081024586677, -118.39130352185116))
                    .add(new LatLng(33.927077084498386, -118.39130986277655))
                    .add(new LatLng(33.92707405365519, -118.39131496827862))
                    .add(new LatLng(33.927066722586623, -118.39131750469446))
                    .add(new LatLng(33.927068689880947, -118.39131823397425))
                    .add(new LatLng(33.927068030419839, -118.39131796208994))
                    .add(new LatLng(33.927065982966624, -118.39132090766913))
                    .add(new LatLng(33.927065258415212, -118.3913193654964))
                    .width(20)
                    .color(Color.RED);
            line = mMap.addPolyline(lineOptions);
            List<Map.Entry> list = new ArrayList<Map.Entry>();
        for (Map.Entry drive : drives.entrySet()) {
            list.add(drive);
        }
        RouteAdapter drivesListAdapter = new RouteAdapter(getActivity(), list);


        } else {
            final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create();

            alertDialogGPS.setTitle("Info");
            alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app.");
            alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
                    startActivity(intentSettings);
                    alertDialogGPS.dismiss();
                }

            });

            alertDialogGPS.show();
        }

    }
    @Override
    public void onResume() {
        super.onResume();
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            mapFrag.getMapAsync(this);
        }
        else {
        }
    }
}

非常感谢任何帮助。

创建RouteAdapter:

public class RouteAdapter extends ArrayAdapter<Map.Entry> {

    private final Activity context;
    // you may need to change List to something else (whatever is returned from drives.entrySet())
    private final List<Map.Entry> drives;
    public static String DriveURL;
    public static String routeLat;
    public static String routeLng;


    SharedPreferences sharedPref;
    // may also need to change List here (above comment)
    public RouteAdapter(Activity context, List<Map.Entry> drives) {
        super(context, R.layout.drive_url_list, drives);
        this.context = context;
        this.drives = drives;
        sharedPref = context.getPreferences(Context.MODE_PRIVATE);
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.third_layout, null, true);

        Map.Entry drive = this.drives.get(position);

        // position is the index of the drives.entrySet() array
        int driveNum = position + 1;

        // need to import your Route class
        Route route = (Route) drive.getValue();
        RouteCoord routeCoord = (RouteCoord) drive.getValue();
        routeLat = routeCoord.getLat();
        routeLng = routeCoord.getLng();

        // need to import your URL class
        DriveURL = route.getUrl();
        System.out.println(DriveURL+"/route.coords");

        return rowView;
    }
}

0 个答案:

没有答案