正在做一个公交查询的应用,打算将选择的车次的所有站点都在地图上用图标标记,然后用直线连接,现在的问题是需要得到每个站点的GeoPoint就得知道每个站点的经纬度,我的公交数据都是取自公交查询网站的数据,但是Google的地名解析不一定每个站点都能解析,例如我作了个demo查询“长沙火车站”无法解析,但是用“长沙站”就可以解析得到经纬度,下面是我用的解析地址的类
public class ConvertUtil
{
public static double[] getLocationInfo(String address)
{
//定义一个HttpClient,用于向指定地址发送请求
HttpClient client = new DefaultHttpClient();
//向指定地址发送Get请求
HttpGet httpGet = new HttpGet("http://maps.google."
+"com/maps/api/geocode/json?address="+address
+"ka&sensor=false");
StringBuilder sb = new StringBuilder();
try
{
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
//循环读取服务器响应
while((b=stream.read())!=-1)
{
sb.append((char)b);
}
//将服务器返回的字符串转换成JSONObject对象
JSONObject jsonObject = new JSONObject(sb.toString());
//从JSONObject对象中取出代表位置的location属性
JSONObject location = jsonObject.getJSONArray("results").getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location");
//获取经纬度信息
double longitude = location.getDouble("lng");
double latitude = location.getDouble("lat");
return new double[]{longitude,latitude};
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public static String getAddress(double longitude,double latitude)
{
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/"
+"geocode/json?latlng="
+latitude+","+longitude
+"&sensor=false®ion=cn");
StringBuilder sb = new StringBuilder();
try
{
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while((b=stream.read())!=-1)
{
sb.append((char) b);
}
JSONObject jsonObj = new JSONObject(sb.toString());
return jsonObj.getJSONArray("results").getJSONObject(0)
.getString("formatted_address");
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
下面是我的显示地图的MapActivity
public class ShowMap extends MapActivity
{
private MapView mapView;
private MapController controller;
private String busLine = "";
private List<Overlay> ol;
private Bitmap posBitmap;
private GeoPoint gp;
private double longitude = 113.05;//经度
private double latitude = 28.18; //纬度
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.browsemap);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
controller = mapView.getController();
Bundle bundle = getIntent().getExtras();
posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pos);
busLine = bundle.getString("BusLine");
refreshMapView(busLine);
} @Override
protected boolean isRouteDisplayed()
{
return false;
}
public void refreshMapView(String busline)
{
String[] results = new String[]{};
String address = "";
double[] positions = new double[]{};
results = busline.split(" - ");
ol = mapView.getOverlays();
ol.clear();
for(int i=0;i<results.length;i++)
{
address = results[i];
positions = ConvertUtil.getLocationInfo(address);
gp = new GeoPoint((int)(positions[1]*1E6),(int)(positions[0]*1E6));
ol.add(new PosOverLay(posBitmap,gp));
}
mapView.displayZoomControls(true);
controller.setZoom(11);
GeoPoint firstGP = new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6));
controller.animateTo(firstGP);
}地图显示的是长沙市,例如旅1路的站点是长沙火车站 - 长岛路口 - 曙光路口 - 韭菜园 - 蔡锷中路口 - 华图教育(太平街口) - 市四医院 - 岳麓山北 - 二里半 - 湖南师大 - 岳麓山南 - 桃子湖 - 湖南大学 - 天马山东 - 科教新村,这些不是每个都能得到解析,求大侠指点啊,不会要自己把每个站点的经纬度解析了吧???
public class ConvertUtil
{
public static double[] getLocationInfo(String address)
{
//定义一个HttpClient,用于向指定地址发送请求
HttpClient client = new DefaultHttpClient();
//向指定地址发送Get请求
HttpGet httpGet = new HttpGet("http://maps.google."
+"com/maps/api/geocode/json?address="+address
+"ka&sensor=false");
StringBuilder sb = new StringBuilder();
try
{
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
//循环读取服务器响应
while((b=stream.read())!=-1)
{
sb.append((char)b);
}
//将服务器返回的字符串转换成JSONObject对象
JSONObject jsonObject = new JSONObject(sb.toString());
//从JSONObject对象中取出代表位置的location属性
JSONObject location = jsonObject.getJSONArray("results").getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location");
//获取经纬度信息
double longitude = location.getDouble("lng");
double latitude = location.getDouble("lat");
return new double[]{longitude,latitude};
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public static String getAddress(double longitude,double latitude)
{
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/"
+"geocode/json?latlng="
+latitude+","+longitude
+"&sensor=false®ion=cn");
StringBuilder sb = new StringBuilder();
try
{
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while((b=stream.read())!=-1)
{
sb.append((char) b);
}
JSONObject jsonObj = new JSONObject(sb.toString());
return jsonObj.getJSONArray("results").getJSONObject(0)
.getString("formatted_address");
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
下面是我的显示地图的MapActivity
public class ShowMap extends MapActivity
{
private MapView mapView;
private MapController controller;
private String busLine = "";
private List<Overlay> ol;
private Bitmap posBitmap;
private GeoPoint gp;
private double longitude = 113.05;//经度
private double latitude = 28.18; //纬度
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.browsemap);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
controller = mapView.getController();
Bundle bundle = getIntent().getExtras();
posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pos);
busLine = bundle.getString("BusLine");
refreshMapView(busLine);
} @Override
protected boolean isRouteDisplayed()
{
return false;
}
public void refreshMapView(String busline)
{
String[] results = new String[]{};
String address = "";
double[] positions = new double[]{};
results = busline.split(" - ");
ol = mapView.getOverlays();
ol.clear();
for(int i=0;i<results.length;i++)
{
address = results[i];
positions = ConvertUtil.getLocationInfo(address);
gp = new GeoPoint((int)(positions[1]*1E6),(int)(positions[0]*1E6));
ol.add(new PosOverLay(posBitmap,gp));
}
mapView.displayZoomControls(true);
controller.setZoom(11);
GeoPoint firstGP = new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6));
controller.animateTo(firstGP);
}地图显示的是长沙市,例如旅1路的站点是长沙火车站 - 长岛路口 - 曙光路口 - 韭菜园 - 蔡锷中路口 - 华图教育(太平街口) - 市四医院 - 岳麓山北 - 二里半 - 湖南师大 - 岳麓山南 - 桃子湖 - 湖南大学 - 天马山东 - 科教新村,这些不是每个都能得到解析,求大侠指点啊,不会要自己把每个站点的经纬度解析了吧???
解决方案 »
- android NDK开发中使用'JNI_CreateJavaVM'的问题
- android4.1 状态栏Wifi信号显示与Setting里面的信号强度显示不一致
- android联系人应用如何调到数据库操作的?
- getResources()方法是如何实现的?
- 关于动态修改menu菜单的问题.
- 求代码
- android应用发布问题
- 像美团的首页,下拉动态加载怎么实现
- AS3.4.1安装完之后make project失败,提示 could not get,后面是 dl.google.com
- 救命!Androidstudio安了好几次都不能正常运行
- Android 手机上 如何实现 ssl 传输?
- 如何获得google map 点击时所点击位置的地理信息
1、 必须得有公路信息(如果站点之间的连线只是直线就不需要了)
2、必须得知道每个站点的经纬度
3、直接用Graphic把所有的站点绘制出来,然后点之间两两连接就可以
mapabc,baidu,sogou,tencent 什么的
还有就是,用google map的时候,偏差很大,不知楼主有没有碰到了