Java 当前位置和搜索位置之间的道路/路线图
以下是执行两点之间的道路路径或路线路径的代码Java 当前位置和搜索位置之间的道路/路线图,java,android,android-layout,android-mapview,android-maps,Java,Android,Android Layout,Android Mapview,Android Maps,以下是执行两点之间的道路路径或路线路径的代码(原点,当前位置){geoPoint,“current location”}到目的地。{geoPoint1,“Desti Location2”}。这对我来说很好 我的问题是如何在当前位置到搜索位置之间创建道路路径。这里我点击了按钮搜索位置的onClick,它显示得非常完美,但我在参考-->{current location(scrgeoPoint)到搜索位置(destgeopointsearch)}之间的路线图中发现了错误 有人有什么想法吗 //se
(原点,当前位置){geoPoint,“current location”}
到目的地。{geoPoint1,“Desti Location2”}
。这对我来说很好
我的问题是如何在当前位置到搜索位置之间创建道路路径。这里我点击了按钮搜索位置的onClick
,它显示得非常完美,但我在参考-->{current location(scrgeoPoint)到搜索位置(destgeopointsearch)}
之间的路线图中发现了错误
有人有什么想法吗
//search button onclick
mapLocationBtn = (Button) findViewById(R.id.locationbutton);
mapLocationBtn.setVisibility(View.INVISIBLE);
mapLocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String usrLoc = autoCompletetextView.getText().toString();
Geocoder gc = new Geocoder(PlacesMapActivity.this);
try {
addr=gc.getFromLocationName(usrLoc, 3);//maximum 2 results
if(addr.size()>0){
address=addr.get(0);//get the first result
convertLongi=address.getLongitude();
convertLati=address.getLatitude();
geopointsearch = new GeoPoint((int)(convertLati * 1E6), (int)(convertLongi * 1E6));
// Map geopointsearch overlay item
itemizedOverlay2 = new AddItemizedOverlay(drawable_client, PlacesMapActivity.this);
overlayitem = new OverlayItem(geopointsearch, "Your Location3", "R u Searching For me!");
itemizedOverlay2.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay2);
itemizedOverlay2.populateNow();
mc.animateTo(geopointsearch);
// mc.zoomIn();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
// Map overlay item
itemizedOverlay = 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 item
itemizedOverlay1 = new AddItemizedOverlay(drawable_client, this);
overlayitem1 = new OverlayItem(geoPoint1, "Your Location2", "I am ur client!");
itemizedOverlay1.addOverlay(overlayitem1);
mapOverlays.add(itemizedOverlay1);
itemizedOverlay1.populateNow();
// Map near 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 screen
int 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 null
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
// Geopoint to place on map
geoPoin = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// Map overlay item
overlayitem = new OverlayItem(geoPoin, place.name,
place.vicinity);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (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 items
itemizedOverlay.populateNow();
}
DrawPath(geoPoint, geoPoint1, geopointsearch, Color.GREEN, mapView);
// Adjusting the zoom level so that you can see all the markers on map
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
// Showing the center of the map
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mc.animateTo(geoPoint1);
mapView.postInvalidate();
}
private 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);
// calculate the distance between 2 point
JSONArray legs = routes.getJSONArray("legs");
Log.d("JSON","legs: "+legs.toString());
JSONObject steps = legs.getJSONObject(0);
Log.d("JSON","steps: "+steps.toString());
JSONObject distance = steps.getJSONObject("distance");
Log.d("JSON","distance: "+distance.toString());
String sDistance = distance.getString("text");
int iDistance = distance.getInt("value");
distextView = (TextView) findViewById(R.id.distextView);
distextView.setText(distance.getString("text"));
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();
//Origin
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));
//Destination
urlString.append("&destination=");// to
urlString.append(Double.toString((double) destgeoPoint.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString((double) destgeoPoint.getLongitudeE6() / 1.0E6));
//search location
urlString.append("&destination=");// to
urlString.append(Double.toString((double) destgeopointsearch.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString((double) destgeopointsearch.getLongitudeE6() / 1.0E6));
urlString.append("&mode=walking&sensor=true");
Log.d("xxx", "URL=" + urlString.toString());
return urlString.toString();
}
//单击搜索按钮
mapLocationBtn=(按钮)findViewById(R.id.locationbutton);
mapLocationBtn.setVisibility(视图不可见);
mapLocationBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串usrLoc=autoCompletetextView.getText().toString();
Geocoder gc=新的Geocoder(PlacesMapActivity.this);
试一试{
addr=gc.getFromLocationName(usrLoc,3);//最多2个结果
如果(地址大小()>0){
address=addr.get(0);//获取第一个结果
convertLongi=address.getLongitude();
convertLati=address.getLatitude();
地质点搜索=新的地质点((int)(convertLati*1E6),(int)(convertLongi*1E6));
//地图地理点搜索覆盖项
itemizedOverlay2=新添加的mizedoverlay(可提取的客户机,placesmpactivity.this);
overlayitem=新的overlayitem(地理点搜索,“您的位置3”,“您正在搜索我!”);
项目化覆盖2.添加覆盖(覆盖项);
添加(itemizedOverlay2);
itemizedOverlay2.populateNow();
首席执行官(地质点搜索);
//mc.zoomIn();
}
}捕获(IOE){
e、 printStackTrace();
}
}
});
//地图覆盖项目
itemizedOverlay=新的AddItemizedOverlay(可绘图用户,此用户);
overlayitem=新的overlayitem(地理点,“您的位置”,“就是您!”);
itemizedOverlay.addOverlay(overlayitem);
添加(itemizedOverlay);
itemizedOverlay.populateNow();
//映射客户端覆盖项
itemizedOverlay1=新添加的ItemizedOverlay(可提取的客户机,此);
overlayitem1=新的OverlayItem(地理点1,“您的位置2”,“我是您的客户!”);
itemizedOverlay1.addOverlay(overlayitem1);
添加(itemizedOverlay1);
itemizedOverlay1.populateNow();
//覆盖项附近的贴图
itemizedOverlay=新添加的itemizedOverlay(可绘制,此);
mc=mapView.getController();
//这些值用于获取地图边界区域
//您可以在屏幕上看到所有标记的区域
int minLat=Integer.MAX_值;
int minLong=Integer.MAX_值;
int maxLat=整数.MIN_值;
int maxLong=整数.MIN_值;
//如果为空,请检查是否为空
if(nearPlaces.results!=null){
//环游所有的地方
用于(地点:nearPlaces.results){
纬度=place.geometry.location.lat;//纬度
经度=place.geometry.location.lng;//经度
//要在地图上放置的地质点
geoPoin=新的地质点((int)(纬度*1E6),
(int)(经度*1E6));
//地图覆盖项目
overlayitem=新的overlayitem(geoPoin,place.name,
地点(邻近地区),;
itemizedOverlay.addOverlay(overlayitem);
//计算地图边界面积
minLat=(int)Math.min(geoPoin.getlatudee6(),minLat);
minLong=(int)Math.min(geoPoin.getLongitudeE6(),minLong);
maxLat=(int)Math.max(geoPoin.getlatudee6(),maxLat);
maxLong=(int)Math.max(geoPoin.getLongitudeE6(),maxLong);
}
添加(itemizedOverlay);
//显示所有覆盖项
itemizedOverlay.populateNow();
}
绘制路径(地质点、地质点1、地质点搜索、Color.GREEN、mapView);
//调整缩放级别,以便可以看到地图上的所有标记
zoomToSpan(Math.abs(minLat-maxLat)、Math.abs(minLong-maxLong));
//显示地图的中心
mc.animateTo(新地质点((maxLat+minLat)/2,(maxLong+minLong)/2);
首席执行官(地质点1);
mapView.postInvalidate();
}
专用空心绘制路径(地质点scrgeoPoint、地质点DesGeopPoint、地质点DesGeopPointSearch、int绿色、地图视图mMapView){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(makeUrl(scrgeoPoint、destgeoPoint、destgeopointsearch));
HttpResponse响应;
试一试{
response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
InputStream=null;
is=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
reader.close();
字符串结果=sb.toString();
JSONObject JSONObject=新JSONObject(结果);
JSONArray routarray=jsonObject.getJSONArray(“路由”);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
startActivity(intent);
private List<Overlay> mapOverlays;
private Projection projection;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
GeoPoint gP1 = new GeoPoint(19240000,-99120000);
GeoPoint gP2 = new GeoPoint(37423157, -122085008);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}