このコードを使用して、2 点間の運転方向を描画しています。私のマップはうまくロードされており、解析中にポイントが取得されています。
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
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.Menu;
import com.esysolutions.dilosys.Map.DirectionsJSONParser;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
public class LocationDrivingMap extends FragmentActivity {
GoogleMap map;
ArrayList<LatLng> markerPoints;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationdrivingdirection);
markerPoints = new ArrayList<LatLng>();
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
map = fm.getMap();
if (map != null) {
map.setMyLocationEnabled(true);
String url = getDirectionsUrl();
Log.d("............FULL URL...................", url);
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(url);
}
}
private String getDirectionsUrl() {
String parameters = "origin=13.687140112679154,100.535258688032630&destination=13.683660045847258,100.53900808095932&sensor=false&units=metric&mode=driving";
String output = "json";
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;
}
// 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) {
Log.d(">>>>>>>Inside DownloadTask<<<<<<<<<", url[0]);
// For storing data from web service
String data = "";
try {
// Fetching the data from web service
data = downloadUrl(url[0]);
} catch (Exception e) {
Log.d("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();
// 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;
try {
jObject = new JSONObject(jsonData[0]);
DirectionsJSONParser parser = new DirectionsJSONParser();
// Starts parsing data
routes = parser.parse(jObject);
Log.d("++++++++++++++++++++Route", "" + routes);
} 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;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// 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);
points.add(position);
Log.d("??????????????? Points", "" + points);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
Log.d("************************* LineOptions", ""
+ lineOptions);
lineOptions.width(2);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
map.addPolyline(lineOptions);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
13.687140112679154, 100.535258688032630), 2.0f));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ポイント内のログはこの値を取得しますが、パスはここに表示されません。
[緯度/経度: (13.68719,100.53523)、緯度/経度: (13.68723,100.53524)、緯度/経度: (13.68728,100.53526)、緯度/経度: (13.68728,100.53526)、緯度/経度: (13.68725,550) 、緯度/経度: (13.68715,100.53573)、緯度/経度: (13.68709,100.53591)、緯度/経度: (13.68699,100.53616)、緯度/経度: (13.68693,100.53629)、緯度/経度: (13.600)6839,13 、緯度/経度: (13.68679,100.5365)、緯度/経度: (13.68666,100.53666)、緯度/経度: (13.68656,100.53679)、緯度/経度: (13.68643,100.53691)、緯度/経度: (13.687)253,10. 、緯度/経度: (13.68554,100.5375)、緯度/経度: (13.68509,100.5378)、緯度/経度: (13.68478,100.53804)、緯度/経度: (13.68469,100.53812)、緯度/経度: (13.68461,180) 、緯度/経度: (13.68456,100.53823)、緯度/経度: (13.68449,100.53828)、緯度/経度: (13.68439,100.53837)、緯度/経度: (13.68406,100.53868)、緯度/経度: (13.684)381,19 、緯度/経度: (13.68371,100.53905)]