Android 如何像运行中的应用程序一样,使用Google Maps API跟踪路径?

Android 如何像运行中的应用程序一样,使用Google Maps API跟踪路径?,android,json,google-maps,Android,Json,Google Maps,我正在尝试像运行中的应用程序一样实现路径跟踪。一旦我的用户加载应用程序并单击按钮,会话将开始记录位置更新。我正在使用 LocationServices.FusedLocationApi.getLastLocation(mgoogleapClient)。我使用JSON数组和对象保存数据 if (myPositions == null) { myPositions = new JSONArray(); } JSONObject myPositi

我正在尝试像运行中的应用程序一样实现路径跟踪。一旦我的用户加载应用程序并单击按钮,会话将开始记录位置更新。我正在使用
LocationServices.FusedLocationApi.getLastLocation(mgoogleapClient)
。我使用JSON数组和对象保存数据

        if (myPositions == null)
    {
        myPositions = new JSONArray();
    }
    JSONObject myPosition = new JSONObject();
    try {
        myPosition.put("lat",currentLatitude);
        myPosition.put("long",currentLongitude);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    myPositions.put(myPosition);
而我正通过

for (int i=0; i < myPositions.length(); i++) {
            JSONObject obj = myPositions.getJSONObject(i);
            long latitude = obj.getLong("lat");
            long longitude = obj.getLong("long");
for(int i=0;i
现在,我如何使用这些值来跟踪用户覆盖的路径


我知道我可以使用google maps roads api和多段线来跟踪路径。使用roads api的多段线会捕捉到道路,因此我可以实现我的目标。但是,roads api文档使用javascript和http URL,我不知道如何实现这两个功能。有人能帮我吗?

我使用了google maps roads api它向服务器发送一个带有坐标的http请求,返回的结果是另一组坐标,对应于一条道路

            stringUrl = "https://roads.googleapis.com/v1/snapToRoads?path=" + old_latitude + ","
                + old_longitude + "|" + currentLatitude + "," + currentLongitude +
                "&interpolate=true&key=" + key;
        ConnectivityManager connMgr = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            new WebTask().execute(stringUrl);
        } else {
            Toast.makeText(getActivity(), "No network connection available.", Toast.LENGTH_LONG);
        }
上面的代码使用Webtask()函数发送http请求

 private class WebTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

    private String downloadUrl(String url) throws IOException {

        InputStream is = null;


        try {
            URL urlx = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlx.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            Log.d("flip", "The response is: " + response);
            is = conn.getInputStream();
            //  Log.d("flip is", String.valueOf(is));
            // Convert the InputStream into a string
            String contentAsString = readIt(is);
            Log.d("flip content", contentAsString);
            return contentAsString;


            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    private String readIt(InputStream stream) throws IOException {

        // Reader reader = new InputStreamReader(stream, "UTF-8");
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        StringBuilder stringBuilder = new StringBuilder();
        String ch;
        while((ch = streamReader.readLine())!=null)
        {
            stringBuilder.append(ch);
        }
        return stringBuilder.toString();
    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {

        Log.d("flip", result);

        double old_lat = 0, old_long = 0;

        try {

            JSONObject mainObj = new JSONObject(result);
            JSONArray jsonarray =mainObj.getJSONArray("snappedPoints");

            for (int i = 0; i < jsonarray.length(); i++)

            {
                JSONObject arrayElem = jsonarray.getJSONObject(i);
                JSONObject locationa = arrayElem.getJSONObject("location");
                double lati = locationa.getDouble("latitude"); //save it somewhere
                double longi = locationa.getDouble("longitude"); //save it somewhere
                Log.d("flip lat", String.valueOf(lati));
                Log.d("flip long", String.valueOf(longi));

                if (old_lat != 0 && old_long != 0) {
                    Polyline line = mMap.addPolyline(new PolylineOptions()
                            .add(new LatLng(old_lat, old_long), new LatLng(lati, longi))
                            .width(10));
                }
                old_lat = lati;
                old_long = longi;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
私有类WebTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
//params来自execute()调用:params[0]是url。
试一试{
返回下载URL(URL[0]);
}捕获(IOE异常){
return“无法检索网页。URL可能无效。”;
}
}
私有字符串下载url(字符串url)引发IOException{
InputStream=null;
试一试{
URL urlx=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)urlx.openConnection();
conn.setReadTimeout(10000/*毫秒*/);
conn.setConnectTimeout(15000/*毫秒*/);
conn.setRequestMethod(“GET”);
conn.setDoInput(真);
//启动查询
连接();
int response=conn.getResponseCode();
Log.d(“翻转”,“响应为:”+响应);
is=conn.getInputStream();
//Log.d(“flip is”,String.valueOf(is));
//将InputStream转换为字符串
字符串contentAsString=readIt(is);
Log.d(“翻转内容”,contentAsString);
返回contentAsString;
//确保在应用程序启动后关闭InputStream
//用完了。
}最后{
如果(is!=null){
is.close();
}
}
}
私有字符串readIt(InputStream)引发IOException{
//读卡器=新的InputStreamReader(流,“UTF-8”);
BufferedReader streamReader=新的BufferedReader(新的InputStreamReader(流,“UTF-8”));
StringBuilder StringBuilder=新的StringBuilder();
弦ch;
而((ch=streamReader.readLine())!=null)
{
stringBuilder.append(ch);
}
返回stringBuilder.toString();
}
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(字符串结果){
Log.d(“翻转”,结果);
双旧长=0,旧长=0;
试一试{
JSONObject mainObj=新的JSONObject(结果);
JSONArray JSONArray=mainObj.getJSONArray(“快照点”);
for(int i=0;i

就是这样!它也会绘制出来!

我很好奇您在Windows 10上使用什么IDE开发应用程序?Android Studio