我不记得我从哪里获取它的信息,但这些课程对我有很大帮助,其中包括如果您想绘制路线。
GoogleParser.java
(谷歌解析器.java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.maps.GeoPoint;
import com.monumentos.log.Logger;
public class GoogleParser extends XMLParser implements Parser {
private int distance;
public GoogleParser(String feedUrl) {
super(feedUrl);
}
public Route parse() {
final String result = convertStreamToString(this.getInputStream());
final Route route = new Route();
final Segment segment = new Segment();
try {
final JSONObject json = new JSONObject(result);
final JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0);
final JSONObject leg = jsonRoute.getJSONArray("legs").getJSONObject(0);
final JSONArray steps = leg.getJSONArray("steps");
final int numSteps = steps.length();
route.setName(leg.getString("start_address") + " to " + leg.getString("end_address"));
route.setCopyright(jsonRoute.getString("copyrights"));
route.setLength(leg.getJSONObject("distance").getInt("value"));
if (!jsonRoute.getJSONArray("warnings").isNull(0)) {
route.setWarning(jsonRoute.getJSONArray("warnings").getString(0));
}
for (int i = 0; i < numSteps; i++) {
final JSONObject step = steps.getJSONObject(i);
final JSONObject start = step.getJSONObject("start_location");
final GeoPoint position = new GeoPoint((int) (start.getDouble("lat")*1E6),
(int) (start.getDouble("lng")*1E6));
segment.setPoint(position);
final int length = step.getJSONObject("distance").getInt("value");
distance += length;
segment.setLength(length);
segment.setDistance(distance/1000);
segment.setInstruction(step.getString("html_instructions").replaceAll("<(.*?)*>", ""));
route.addPoints(decodePolyLine(step.getJSONObject("polyline").getString("points")));
route.addSegment(segment.copy());
}
} catch (JSONException e) {
Logger.appendLog( "Google JSON Parser - " + feedUrl);
}
return route;
}
private static String convertStreamToString(final InputStream input) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(input));
final StringBuilder sBuf = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sBuf.append(line);
}
} catch (IOException e) {
Logger.appendLog( "Google parser, stream2string");
} finally {
try {
input.close();
} catch (IOException e) {
Logger.appendLog( "Google parser, stream2string");
}
}
return sBuf.toString();
}
private List<GeoPoint> decodePolyLine(final String poly) {
int len = poly.length();
int index = 0;
List<GeoPoint> decoded = new ArrayList<GeoPoint>();
int lat = 0;
int lng = 0;
while (index < len) {
int b;
int shift = 0;
int result = 0;
do {
b = poly.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 = poly.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
decoded.add(new GeoPoint(
(int) (lat*1E6 / 1E5), (int) (lng*1E6 / 1E5)));
}
return decoded;
}
}
Parser.java
public interface Parser {
public Route parse();
}
Route.java
import java.util.ArrayList;
import java.util.List;
import com.google.android.maps.GeoPoint;
public class Route {
private String name;
private final List<GeoPoint> points;
private List<Segment> segments;
private String copyright;
private String warning;
private String country;
private int length;
private String polyline;
public Route() {
points = new ArrayList<GeoPoint>();
segments = new ArrayList<Segment>();
}
public void addPoint(final GeoPoint p) {
points.add(p);
}
public void addPoints(final List<GeoPoint> points) {
this.points.addAll(points);
}
public List<GeoPoint> getPoints() {
return points;
}
public void addSegment(final Segment s) {
segments.add(s);
}
public List<Segment> getSegments() {
return segments;
}
public void setName(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public String getCopyright() {
return copyright;
}
public void setWarning(String warning) {
this.warning = warning;
}
public String getWarning() {
return warning;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
public void setLength(int length) {
this.length = length;
}
public int getLength() {
return length;
}
public void setPolyline(String polyline) {
this.polyline = polyline;
}
public String getPolyline() {
return polyline;
}
}
RouteOverlay.java
public class RouteOverlay extends Overlay {
private final List<GeoPoint> routePoints;
private int colour;
private static final int ALPHA = 200;
private static final float STROKE = 4.5f;
private final Path path;
private final Point p;
private final Paint paint;
public RouteOverlay(final Route route, final int defaultColour) {
super();
routePoints = route.getPoints();
colour = defaultColour;
path = new Path();
p = new Point();
paint = new Paint();
}
@Override
public final void draw(final Canvas c, final MapView mv,
final boolean shadow) {
super.draw(c, mv, shadow);
paint.setColor(colour);
paint.setAlpha(ALPHA);
paint.setAntiAlias(true);
paint.setStrokeWidth(STROKE);
paint.setStyle(Paint.Style.STROKE);
redrawPath(mv);
c.drawPath(path, paint);
}
public final void setColour(final int c) {
colour = c;
}
public final void clear() {
routePoints.clear();
}
private void redrawPath(final MapView mv) {
final Projection prj = mv.getProjection();
path.rewind();
final Iterator<GeoPoint> it = routePoints.iterator();
prj.toPixels(it.next(), p);
path.moveTo(p.x, p.y);
while (it.hasNext()) {
prj.toPixels(it.next(), p);
path.lineTo(p.x, p.y);
}
path.setLastPoint(p.x, p.y);
}
}
Segment.java
import com.google.android.maps.GeoPoint;
public class Segment {
private GeoPoint start;
private String instruction;
private int length;
private double distance;
public Segment() {
}
public void setInstruction(final String turn) {
this.instruction = turn;
}
public String getInstruction() {
return instruction;
}
public void setPoint(final GeoPoint point) {
start = point;
}
public GeoPoint startPoint() {
return start;
}
public Segment copy() {
final Segment copy = new Segment();
copy.start = start;
copy.instruction = instruction;
copy.length = length;
copy.distance = distance;
return copy;
}
public void setLength(final int length) {
this.length = length;
}
public int getLength() {
return length;
}
public void setDistance(double distance) {
this.distance = distance;
}
public double getDistance() {
return distance;
}
}
XMLParser.java
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import com.monumentos.log.Logger;
public class XMLParser {
protected static final String MARKERS = "markers";
protected static final String MARKER = "marker";
protected URL feedUrl;
protected XMLParser(final String feedUrl) {
try {
this.feedUrl = new URL(feedUrl);
} catch (MalformedURLException e) {
Logger.appendLog( "XML parser - " + feedUrl);
}
}
protected InputStream getInputStream() {
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
Logger.appendLog( "XML parser - " + feedUrl);
return null;
}
}
}
希望这对您有所帮助。
调用 API 的示例:
Parser parser;
String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
final StringBuffer sBuf = new StringBuffer(jsonURL);
sBuf.append("origin=");
sBuf.append( points.get(0).getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(points.get(0).getLongitudeE6()/1E6);
sBuf.append("&destination=");
sBuf.append(points.get(points.size()-1).getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(points.get(points.size()-1).getLongitudeE6()/1E6);
sBuf.append("&sensor=true&mode="+routeMode);
Logger.appendLog(sBuf.toString());
parser = new GoogleParser(sBuf.toString());
Route r = parser.parse();
String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
。 - ƒernando Valle