让公共交通路线在谷歌地图 [英] make route of public transportation in google maps

查看:127
本文介绍了让公共交通路线在谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为自己在我的城市公共交通导航的应用程序,但我不知道我应该怎么用绘制在谷歌地图的路线,任何人可以给予解决?

I want to make an application about public transportation navigator in my city, but I dont know what should I use to draw the route in google maps, can anybody give the solution?

是A到B和B到A是只使用一条路径,或将其扩大两倍?如何导入到谷歌地图?我使用Android的工作室了。

are A to B and B to A is just use one route or make it two? and how to import that to google maps? I use android studio too.

推荐答案

在这条路上你。
这是第一个静态类没有改变这一类相同的复制和放大器;糊状。

thy on this way. this is first static class nothing to change this class same to copy & paste.

package com.ibl.googlemap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;

public class DirectionsJSONParser {

/**
 * Receives a JSONObject and returns a list of lists containing latitude and
 * longitude
 */
public List<List<HashMap<String, String>>> parse(JSONObject jObject) {

    List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>();
    JSONArray jRoutes = null;
    JSONArray jLegs = null;
    JSONArray jSteps = null;

    try {

        jRoutes = jObject.getJSONArray("routes");

        /** Traversing all routes */
        for (int i = 0; i < jRoutes.length(); i++) {
            jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
            List path = new ArrayList<HashMap<String, String>>();

            /** Traversing all legs */
            for (int j = 0; j < jLegs.length(); j++) {
                jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps");

                /** Traversing all steps */
                for (int k = 0; k < jSteps.length(); k++) {
                    String polyline = "";
                    polyline = (String) ((JSONObject) ((JSONObject) jSteps
                            .get(k)).get("polyline")).get("points");
                    List<LatLng> list = decodePoly(polyline);

                    /** Traversing all points */
                    for (int l = 0; l < list.size(); l++) {
                        HashMap<String, String> hm = new HashMap<String, String>();
                        hm.put("lat",
                                Double.toString(((LatLng) list.get(l)).latitude));
                        hm.put("lng",
                                Double.toString(((LatLng) list.get(l)).longitude));
                        path.add(hm);
                    }
                }
                routes.add(path);
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
    }

    return routes;
}

/**
 * Method to decode polyline points Courtesy :
 * http://jeffreysambells.com/2010
 * /05/27/decoding-polylines-from-google-maps-direction-api-with-java
 * */
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;
}
}

听到这个类来读取正确的一些评论。

hear this class to read proper some comment.

package com.ibl.googlemap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.ibl.comman.GPSTracker;
import com.ibl.foodpulse.CreateShareDeal;
import com.ibl.foodpulse.R;

public class ViewMapDirection extends FragmentActivity implements     OnClickListener {

private GoogleMap map;
private ArrayList<LatLng> markerPoints;
private Button btnShow;
private double ans;

private GPSTracker gps; // this is current location
private double latitude, longitude;
// this is user current location lat lng
private double longitude2, latitude2;
private int bcid;
private int vuid;
String res = "";
String url2;

Boolean checknet = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.googlemap_only_activity);
    gps = new GPSTracker(ViewMapDirection.this);

    Bundle b = getIntent().getExtras();
    latitude = b.getFloat("lat"); // hear i got lat lng from last activity.
    longitude = b.getFloat("longy");
    Log.e("View Map Direction", "get the Lat and Lng :" + latitude + " :: " + longitude);

    if (gps.canGetLocation()) {
        // please set here your current location and hide this method
        latitude2 = gps.getLatitude();
        longitude2 = gps.getLongitude();
        // hear i will got current location from GPS Tracker
        Log.e("View Map Direction ", " get Current Lat lng " + latitude2 + " lng " + longitude2);
    } else {
        gps.showSettingsAlert(ViewMapDirection.this, ViewMapDirection.this);
    }

    Intent j = getIntent();
    bcid = j.getIntExtra("BCARD_ID", 0);
    vuid = j.getIntExtra("VUID1", 0);
    btnShow = (Button) findViewById(R.id.btn_onlywebview_close);
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map_googlemapAct);

    // Getting Map for the SupportMapFragment
    map = fm.getMap();
    /*
     * db=new MyDBHandler(this); gp=db.getGPSdata(bcid);
     */
    btnShow.setOnClickListener(this);

}

// Initializing
private String getDirectionsUrl(LatLng origin, LatLng dest) {

    // Origin of route
    String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    // Sensor enabled
    String sensor = "sensor=false";

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor;

    // Output format
    String output = "json";

    // Start

    int Radius = 6371;// radius of earth in Km
    double lat1 = origin.latitude;
    double lat2 = dest.latitude;
    double lon1 = origin.longitude;
    double lon2 = dest.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec + " Meter   " + meterInDec);
    ans = Radius * c;
    // Toast.makeText(getApplicationContext(), "Result" + ans,
    // Toast.LENGTH_SHORT).show();

    // End

    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

    return url;
}

/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL(strUrl);

        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();

        // Connecting to url
        urlConnection.connect();

        // Reading data from url
        iStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

        StringBuffer sb = new StringBuffer();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        data = sb.toString();

        br.close();

    } catch (Exception e) {
        // Log.d("Exception while downloading url", e.toString());
    } finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    markerPoints = new ArrayList<LatLng>();

    // Getting reference to SupportMapFragment of the
    // activity_main

    if (map != null) {
        Log.e(" in View Map ", " in On Resume Method calling .........");
        // if (gps.canGetLocation()) {
        // latitude2 = gps.getLatitude();
        // longitude2 = gps.getLongitude();

        // Enable MyLocation Button in the Map
        map.setMyLocationEnabled(true);
        // Toast.makeText(getApplicationContext(),"Card Lattitude is
        // "+gp.getGps_lattitude()+" LOngitude is
        // "+gp.getGps_longitude(),Toast.LENGTH_LONG).show();
        LatLng CardPoint = new LatLng(latitude, longitude);
        // LatLng CardPoint=new
        // LatLng(21.237425849325856,72.8330023214221);

        Log.e(" in View Map ", " in On Card Point........." + CardPoint.latitude);

        MarkerOptions Destpoint = new MarkerOptions();
        Destpoint.position(CardPoint);
        map.addMarker(Destpoint);
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(CardPoint, 10));
        // Toast.makeText(getApplicationContext(),"Visitor Lattitude is
        // "+latitude2+" LOngitude is
        // "+longitude2,Toast.LENGTH_LONG).show();
        LatLng UserPoint = new LatLng(latitude2, longitude2);
        MarkerOptions Sourcepoint = new MarkerOptions();
        Sourcepoint.position(UserPoint);
        map.addMarker(Sourcepoint);

        String url = getDirectionsUrl(UserPoint, CardPoint);
        DownloadTask downloadTask = new DownloadTask();
        downloadTask.execute(url);
        // } else {
        // gps.showSettingsAlert(ViewMapDirection.this);
        // }
    }
}

// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, String> {

    // Downloading data in non-ui thread
    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";
        Log.e(" in View Map ", " in Download Task  ........." + url.toString());
        try {
            // Fetching the data from web service
            data = downloadUrl(url[0]);
        } catch (Exception e) {
            Log.e("Background Task", e.toString());
        }
        return data;
    }

    // Executes in UI thread, after the execution of
    // doInBackground()
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        ParserTask parserTask = new ParserTask();
        Log.e(" in View Map ", " in on Post  .........");
        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);
    }
}

/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;
        Log.e(" in View Map ", " in parser Task Calling  .........");
        try {
            jObject = new JSONObject(jsonData[0]);
            DirectionsJSONParser parser = new DirectionsJSONParser();
            // Starts parsing data
            routes = parser.parse(jObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return routes;
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();
        Log.e(" in View Map ", " in parser post ........." + result.size());
        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            Log.e(" in View Map ", " in Result For .........");
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            Log.e(" in View Map ", " in path  ........." + path.size());
            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                Log.e("in View Direction ", " lag ,,,,, " + lat + " lng ... " + lng);
                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(4);
            lineOptions.color(Color.BLUE);
        }

        // Drawing polyline in the Google Map for the i-th route
        // if (gps.canGetLocation()) {
        // latitude2 = gps.getLatitude();
        // longitude2 = gps.getLongitude();
        if (map != null) {
            if (gps.canGetLocation()) {
                latitude2 = gps.getLatitude();
                longitude2 = gps.getLongitude();

                Log.e("View Map Direction ", " get Current Lat lng " + latitude2 + " lng " + longitude2);
                if ((latitude != 0) && (longitude != 0)) {
                    map.addPolyline(lineOptions);
                }
            } else {
                gps.showSettingsAlert(ViewMapDirection.this, ViewMapDirection.this);
            }
        }
        // } else {
        // gps.showSettingsAlert(ViewMapDirection.this);
        // }
        // onResume();
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_onlywebview_close:
        finish();
        break;
    default:
        break;
    }

}

}

在因为这个类是在我的当前项目运行适当的阅读评论,并按照上面的类。
这一套在你的manifest文件

in above class read proper to comment and follow it because this class is running in my current project. this set at your manifest file

 <provider
        android:name="com.facebook.NativeAppCallContentProvider"
        android:authorities="com.facebook.app.NativeAppCallContentProvider1621374528084848"
        android:exported="true" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your key" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

和ü知道添加还需要Google Play服务库。

and u know to add also need to google play services lib.

这篇关于让公共交通路线在谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆