android– 任何人都可以指导我如何使用JSON在两个位置之间获取Google方向

android– 任何人都可以指导我如何使用JSON在两个位置之间获取Google方向,第1张

概述我听说自2012年7月27日以来KML文件已不再可用(因为Google已经改变了检索Google方向的结构,现在你只能通过JSON或XML获取它)所以PLZ指导我如何在2个位置之间建立道路.以下代码工作精细NW享受GUYS这是我的代码:我提到这个链接:http://www.androidhive.info/2012/08/android-working-

我听说自2012年7月27日以来KML文件已不再可用(因为Google已经改变了检索Google方向的结构,现在你只能通过JSON或XML获取它)
所以PLZ指导我如何在2个位置之间建立道路.

以下代码工作精细NW享受GUYS
这是我的代码:

我提到这个链接:http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/
PlacesMapActivity.java

在下面的代码中,它显示2点之间的直线.

public class PlacesMapActivity extends MapActivity {@OverrIDepublic voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.map_places);Intent i = getIntent(); // Getting intent data  String user_latitude = i.getStringExtra("user_latitude");// Users current geo locationString user_longitude = i.getStringExtra("user_longitude");//Userscurrent geo location      nearPlaces = (PlacesList) i.getSerializableExtra("near_places");// Nearplaces ListmapVIEw = (MapVIEw) findVIEwByID(R.ID.mapVIEw);mapVIEw.setBuiltInZoomControls(true);mapOverlays = mapVIEw.getoverlays();geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),                              (int) (Double.parseDouble(user_longitude) * 1E6));// Geopoint to place on mapgeoPoint1 = new GeoPoint((int) (Double.parseDouble("12.930621") * 1E6),            (int) (Double.parseDouble("77.581710") * 1E6));//Geopoint to ClIEntplace on map     Drawable drawable_user = this.getResources()    .getDrawable(R.drawable.mark_red);      // Drawable marker icon     Drawable drawable_clIEnt = this.getResources()     .getDrawable(R.drawable.mark_blue);     // Drawable ClIEnt icon Drawable drawable = this.getResources()    .getDrawable(R.drawable.clIEntflag);    // Drawable marker icon// Map overlay itemitemizedoverlay = new Additemizedoverlay(drawable_user, this);overlayitem = new OverlayItem(geoPoint, "Your Location","That is you!");itemizedoverlay.addOverlay(overlayitem);        mapOverlays.add(itemizedoverlay);itemizedoverlay.populateNow();      // Map clIEnt overlay itemitemizedoverlay1 = new Additemizedoverlay(drawable_clIEnt, this);       overlayitem1 = new OverlayItem(geoPoint1, "Your Location2", "I am ur clIEnt!");itemizedoverlay1.addOverlay(overlayitem1);              mapOverlays.add(itemizedoverlay1);itemizedoverlay1.populateNow();// Map clIEnt overlay item  itemizedoverlay = new Additemizedoverlay(drawable, this);mc = mapVIEw.getController();// These values are used to get map boundary area// The area where you can see all the markers on screenint minLat = Integer.MAX_VALUE;int minLong = Integer.MAX_VALUE;int maxLat = Integer.MIN_VALUE;int maxLong = Integer.MIN_VALUE;// check for null in case it is nullif (nearPlaces.results != null) {// loop through all the placesfor (Place place : nearPlaces.results) {latitude = place.geometry.location.lat; // latitudelongitude = place.geometry.location.lng; // longitude// Geopoint to place on mapgeoPoin = new GeoPoint((int) (latitude * 1E6),    (int) (longitude * 1E6));// Map overlay itemoverlayitem = new OverlayItem(geoPoin, place.name,    place.vicinity);itemizedoverlay.addOverlay(overlayitem);// calculating map boundary areaminLat  = (int) Math.min( geoPoin.getLatitudeE6(), minLat );minLong = (int) Math.min( geoPoin.getLongitudeE6(), minLong);maxLat  = (int) Math.max( geoPoin.getLatitudeE6(), maxLat );maxLong = (int) Math.max( geoPoin.getLongitudeE6(), maxLong );}mapOverlays.add(itemizedoverlay);// showing all overlay itemsitemizedoverlay.populateNow();}// Adjusting the zoom level so that you can see all the markers on mapmapVIEw.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong -     maxLong ));// Showing the center of the mapmc.animateto(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));mc.animateto(geoPoint1);//mapVIEw.postInvalIDate();myoverlay = new MyOverlay();mapOverlays.add(myoverlay); }@OverrIDepublic boolean onCreateOptionsMenu(Menu menu){MenuInflater menuInflater = getMenuInflater();menuInflater.inflate(R.menu.activity_main, menu);return true;}/*** Event Handling for IndivIDual menu item selected* IDentify single menu item by it's ID* */@OverrIDepublic boolean onoptionsItemSelected(MenuItem item){switch (item.getItemID()){case R.ID.mylocation:// Single menu item is selected do somethingToast.makeText(PlacesMapActivity.this, "Moving To Current location",      Toast.LENGTH_SHORT).show();// geoPoint.gpsCurrentLocation(); return true;case R.ID.mapStreet:Toast.makeText(PlacesMapActivity.this, "Map normal Street VIEw",   Toast.LENGTH_SHORT).show();if(mapVIEw.isSatellite()==true){mapVIEw.setSatellite(false);}return true;case R.ID.mapSatellite:Toast.makeText(PlacesMapActivity.this, "Map Satellite VIEw",  Toast.LENGTH_SHORT).show();if(mapVIEw.isSatellite()==false){mapVIEw.setSatellite(true);}return true;default:return super.onoptionsItemSelected(item);}}        @OverrIDeprotected boolean isRoutedisplayed() {return false;}add this road routeprivate voID DrawPath(GeoPoint scrgeoPoint, GeoPoint destgeoPoint, GeoPoint destgeopointsearch, int green, MapVIEw mMapVIEw) {     httpClIEnt httpclIEnt = new DefaulthttpClIEnt();        httpPost httppost = new httpPost(makeUrl(scrgeoPoint, destgeoPoint, destgeopointsearch));        httpResponse response;        try {            response = httpclIEnt.execute(httppost);            httpentity entity = response.getEntity();            inputStream is = null;            is = entity.getContent();            BufferedReader reader = new BufferedReader(new inputStreamReader(                    is, "iso-8859-1"), 8);            StringBuilder sb = new StringBuilder();            sb.append(reader.readline() + "\n");            String line = "0";            while ((line = reader.readline()) != null) {                sb.append(line + "\n");            }            is.close();            reader.close();            String result = sb.toString();            JsONObject JsonObject = new JsONObject(result);            JsONArray routeArray = JsonObject.getJsONArray("routes");            JsONObject routes = routeArray.getJsONObject(0);            JsONObject overvIEwpolylines = routes.getJsONObject("overvIEw_polyline");            String encodedString = overvIEwpolylines.getString("points");            List<GeoPoint> pointToDraw = decodepoly(encodedString);            mMapVIEw.getoverlays().add(new MyOverLay(pointToDraw));        } catch (ClIEntProtocolException e) {                        e.printstacktrace();        } catch (IOException e) {            e.printstacktrace();        } catch (Exception e) {            e.printstacktrace();        }    }private List<GeoPoint> decodepoly(String encoded) {     List<GeoPoint> poly = new ArrayList<GeoPoint>();        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;            GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),                    (int) (((double) lng / 1E5) * 1E6));            poly.add(p);        }        return poly;    }private String makeUrl(GeoPoint scrgeoPoint, GeoPoint destgeoPoint, GeoPoint destgeopointsearch) {    StringBuilder urlString = new StringBuilder();    urlString.append("http://maps.GoogleAPIs.com/maps/API/directions/Json");    urlString.append("?origin=");// from    urlString.append(Double.toString((double) scrgeoPoint.getLatitudeE6() / 1.0E6));    urlString.append(",");    urlString.append(Double.toString((double) scrgeoPoint.getLongitudeE6() / 1.0E6));    urlString.append("&destination=");// to    urlString.append(Double.toString((double) destgeoPoint.getLatitudeE6() / 1.0E6));    urlString.append(",");    urlString.append(Double.toString((double) destgeoPoint.getLongitudeE6() / 1.0E6));   /* urlString.append("&destination=");// searchto    urlString.append(Double.toString((double) destgeopointsearch.getLatitudeE6() / 1.0E6));    urlString.append(",");    urlString.append(Double.toString((double) destgeopointsearch.getLongitudeE6() / 1.0E6));    */    urlString.append("&sensor=false");    Log.d("xxx", "URL=" + urlString.toString());    return urlString.toString();}

在最后我有两点之间的道路路线图..添加此代码.

class MyOverLay extends Overlay {private int pathcolor;private final List<GeoPoint> points;private boolean drawStartEnd;public MyOverLay(List<GeoPoint> pointToDraw) {             this(pointToDraw, color.magenta, true);}public MyOverLay(List<GeoPoint> points, int pathcolor, boolean drawStartEnd) {    this.points = points;    this.pathcolor = pathcolor;    this.drawStartEnd = drawStartEnd;}private voID drawoval(Canvas canvas, Paint paint, Point point) {    Paint ovalPaint = new Paint(paint);    ovalPaint.setStyle(Paint.Style.FILL_AND_stroke);    ovalPaint.setstrokeWIDth(2);    ovalPaint.setcolor(color.BLUE);    int _radius = 6;    RectF oval = new RectF(point.x - _radius, point.y - _radius,            point.x + _radius, point.y + _radius);    canvas.drawoval(oval, ovalPaint);}public boolean draw(Canvas canvas, MapVIEw mapVIEw, boolean shadow,        long when) {    Projection projection = mapVIEw.getProjection();    if (shadow == false && points != null) {        Point startPoint = null, endPoint = null;        Path path = new Path();        // We are creating the pathfor (int i = 0; i < points.size(); i++) {GeoPoint gPointA = points.get(i);Point pointA = new Point();projection.topixels(gPointA, pointA);if (i == 0) { // This is the start pointstartPoint = pointA;path.moveto(pointA.x, pointA.y);} else {if (i == points.size() - 1)// This is the end point                endPoint = pointA;            path.lineto(pointA.x, pointA.y);        }    }    Paint paint = new Paint();    paint.setAntiAlias(true);    paint.setcolor(pathcolor);    paint.setStyle(Paint.Style.stroke);    paint.setstrokeWIDth(5);    paint.setAlpha(90);    if (getDrawStartEnd()) {        if (startPoint != null) {            drawoval(canvas, paint, startPoint);        }        if (endPoint != null) {            drawoval(canvas, paint, endPoint);        }    }    if (!path.isEmpty())        canvas.drawPath(path, paint);}return super.draw(canvas, mapVIEw, shadow, when);}public boolean getDrawStartEnd() {return drawStartEnd;}public voID setDrawStartEnd(boolean markStartEnd) {drawStartEnd = markStartEnd;}}}

解决方法:

自Google关闭KML以来,您可以看到我的帖子here以及如何在JsON中完成答案的完整答案.

总结

以上是内存溢出为你收集整理的android – 任何人都可以指导我如何使用JSON在两个位置之间获取Google方向全部内容,希望文章能够帮你解决android – 任何人都可以指导我如何使用JSON在两个位置之间获取Google方向所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1099307.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存