查找路径上的android的两个位置之间的映射 [英] Finding route on a map between two locations in android
本文介绍了查找路径上的android的两个位置之间的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我建立一个应用程序,我需要找到2 locations.The用户之间的映射可以进入2处,即源和目标,他可以得到一个地图上的路由。 有人可以PLZ建议我如何着手dis.Is有一个教程,可以帮助我在这? 感谢名单提前。
解决方案
公共类MappzzActivity扩展MapActivity {
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.mapp);
双src_lat = latPoint;
双src_long = lngPoint;
双dest_lat = 8.88056;
双dest_long = 76.5917;
的GeoPoint srcGeoPoint =新的GeoPoint((INT)(src_lat * 1E6)
(中间体)(src_long * 1E6));
的GeoPoint destGeoPoint =新的GeoPoint((INT)(dest_lat * 1E6)
(中间体)(dest_long * 1E6));
DrawPath(srcGeoPoint,destGeoPoint,Color.GREEN,图形页面);
。mapView.getController()animateTo(srcGeoPoint);
mapView.getController()setZoom(15)。
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
私人无效DrawPath(SRC的GeoPoint,GeoPoint对象DEST,INT的颜色,
图形页面mMapView01){
//连接到地图Web服务
StringBuilder的urlString =新的StringBuilder();
urlString.append(http://maps.google.com/maps?f=d&hl=en);
urlString.append(&放大器; SADDR =); //从
urlString.append(Double.toString((双)src.getLatitudeE6()/ 1.0E6));
urlString.append(,);
urlString
.append(Double.toString((双)src.getLongitudeE6()/ 1.0E6));
urlString.append(&放大器; DADDR =); //给
urlString
.append(Double.toString((双)dest.getLatitudeE6()/ 1.0E6));
urlString.append(,);
urlString
.append(Double.toString((双)dest.getLongitudeE6()/ 1.0E6));
urlString.append(&放大器,即= UTF8&功放; 0安培; OM = 0&放大器;输出= KML);
Log.d(XXX,URL =+ urlString.toString());
//获取KML(XML)文档。并解析它得到的坐标(方向
// 路线)。
文档DOC = NULL;
HttpURLConnection类的URLConnection = NULL;
网址URL = NULL;
尝试 {
URL =新的URL(urlString.toString());
的URLConnection =(HttpURLConnection类)url.openConnection();
urlConnection.setRequestMethod(GET);
urlConnection.setDoOutput(真正的);
urlConnection.setDoInput(真正的);
urlConnection.connect();
DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
DocumentBuilder的DB = dbf.newDocumentBuilder();
文档= db.parse(urlConnection.getInputStream());
如果(doc.getElementsByTagName(GeometryCollection的)的getLength()> 0){
字符串路径= doc.getElementsByTagName(GeometryCollection的)
.item(0).getFirstChild()。getFirstChild()
。.getFirstChild()getNodeValue();
Log.d(XXX,路径=+路径);
的String []对= path.split();
串[] lngLat =双[0] .split(,); // lngLat [0] =经度
// SRC
的GeoPoint startGP =新的GeoPoint(
(中间体)(Double.parseDouble(lngLat [1])* 1E6),
(中间体)(Double.parseDouble(lngLat [0])* 1E6));
mMapView01.getOverlays()
。新增(新MyOverLay(startGP,startGP,1));
的GeoPoint GP1;
的GeoPoint GP2 = startGP;
的for(int i = 1; I< pairs.length;我++){
lngLat =对[I] .split(,);
GP1 = GP2;
// 小心!对于GeoPoint对象,第一:纬度,第二:经度
GP2 =新的GeoPoint(
(中间体)(Double.parseDouble(lngLat [1])* 1E6),
(中间体)(Double.parseDouble(lngLat [0])* 1E6));
mMapView01.getOverlays()。增加(
新MyOverLay(GP1,GP2,2,颜色));
Log.d(XXX,对:+对[I]);
}
。mMapView01.getOverlays()增加(新MyOverLay(DEST,DEST,3));
}
}赶上(MalformedURLException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}赶上(的ParserConfigurationException E){
e.printStackTrace();
}赶上(的SAXException E){
e.printStackTrace();
}
}
}
I am building an app where i need to find the route on a map between 2 locations.The user can enter the 2 locations i.e the source and destination and he could get the route on a map. Can someone plz suggest me as how to proceed with dis.Is there a tutorial which could help me in this? Thanx in advance.
解决方案
public class MappzzActivity extends MapActivity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mapp);
double src_lat = latPoint;
double src_long = lngPoint;
double dest_lat = 8.88056;
double dest_long = 76.5917;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
// connect to map web service
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(Double.toString((double) src.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString
.append(Double.toString((double) src.getLongitudeE6() / 1.0E6));
urlString.append("&daddr=");// to
urlString
.append(Double.toString((double) dest.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString
.append(Double.toString((double) dest.getLongitudeE6() / 1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
Log.d("xxx", "URL=" + urlString.toString());
// get the kml (XML) doc. And parse it to get the coordinates(direction
// route).
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) {
String path = doc.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
Log.d("xxx", "path=" + path);
String[] pairs = path.split(" ");
String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude
// src
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays()
.add(new MyOverLay(startGP, startGP, 1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(
new MyOverLay(gp1, gp2, 2, color));
Log.d("xxx", "pair:" + pairs[i]);
}
mMapView01.getOverlays().add(new MyOverLay(dest, dest, 3));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
这篇关于查找路径上的android的两个位置之间的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文