导航:首页 > 操作系统 > android经纬度转换工具

android经纬度转换工具

发布时间:2024-04-03 01:24:56

android 输入一个地址 转成经纬度 为什么第二次就不进ongetaddrresult

如果打算写一些LBS方面的的应用,可能用到地址与经纬度的互相转换。android.location.Geocoder类提供了此功能。实际上,Geocoder既提供了前向转换,也提供了后向转换——它可以获取地址并返回经纬度,也可以将经纬度对转换为一组地址。

可以通过各种方式来描述位置。例如,getFromLocationName() 方法可以获得地方的名称、物理地址和机场编号,或者该位置的流行名称。这些方法提供了一个地址列表(List<Address>),而不是一个地址。因为这些方法返回多个结果,官方建议最好提供1~5的maxResults 值来限制结果集,下面我们来看一个查询地址的例子,和上次一样我们得自己定义一个类继承MapActivity。

我们可以试着写一个输入地名返回经纬度的程序。

在XML中定义一个输入框,一个按钮,一个MapView.

用户输入一个地名点击按钮,返回经纬度。

这是按钮的OnCliclk方法,其他省略。

public void onClick(View v) {
try {
EditText loc = (EditText) findViewById(R.id.location);
String locationName = loc.getText().toString();
List<Address> addressList = geocoder.getFromLocationName(
locationName, 5);
if (addressList != null && addressList.size() > 0) {
int lat = (int) (addressList.get(0).getLatitude() * 1E6);
int lng = (int) (addressList.get(0).getLongitude() * 1E6);

GeoPoint pt = new GeoPoint(lat, lng);
mapView.getController().setZoom(15);
mapView.getController().setCenter(pt);
}
} catch (Exception e) {
e.printStackTrace();
}
}

这样写感觉是没什么错,实际测试时发现,如果输入“tian an men”就可以返回经纬度,但输入“天安门”就会抛异常,说服务不可以用什么的,网上也有遇到这问题的人,eoe上边也有,但都不了了之了。国外也有输入英文抛异常的,http://code.google.com/p/android/issues/detail?id=8816,

在21楼发现了新的方法,完全放弃了getFromLocationName方法,用的是The Google Geocoding API

这个API整合可以代替先前的方法,输入地名返回JSON或者XML格式的文件,包含一对地址信息。

XML为http://maps.googleapis.com/maps/api/geocode/xml?address=北京交通大学&sensor=false

JSON为http://maps.googleapis.com/maps/api/geocode/json?address=北京交通大学&sensor=false

现在需要自己写一个静态方法去解析XML或者JSON,以JSON为例:

public class MapUtility {
public static JSONObject getLocationInfo(String address) {

HttpGet httpGet = new HttpGet("http://maps.google."

+ "com/maps/api/geocode/json?address=" + address

+ "&sensor=false");

HttpClient client = new DefaultHttpClient();

HttpResponse response;

StringBuilder stringBuilder = new StringBuilder();

try {

response = client.execute(httpGet);

HttpEntity entity = response.getEntity();

InputStream stream = entity.getContent();

int b;

while ((b = stream.read()) != -1) {

stringBuilder.append((char) b);

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

JSONObject jsonObject = new JSONObject();

try {

jsonObject = new JSONObject(stringBuilder.toString());

} catch (JSONException e) {

e.printStackTrace();

}

return jsonObject;

}

// converts JSONObject into a GeoPoint.

public static GeoPoint getGeoPoint(JSONObject jsonObject) {

Double lon = new Double(0);

Double lat = new Double(0);

try {

lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0)

.getJSONObject("geometry").getJSONObject("location")

.getDouble("lng");

lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)

.getJSONObject("geometry").getJSONObject("location")

.getDouble("lat");

} catch (JSONException e) {

e.printStackTrace();

}

return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

}

}

第一个方法根据地址取得JSON,第二个方法从JSON提取坐标。

最后程序效果如下:

Android地址与经纬度转换

最后,应该考虑在不同于UI线程的线程中执行地理编码操作。很明显:这项操作很耗时,而且你一定不希望UI在进行地理编码时停顿,如果停顿会阻塞整个用户界面。当在执行一些耗时的操作的时候,不能及时地分发 事件,包括用户界面重绘事件。从用户的角度来看,应用程序看上去像挂掉了。更糟糕的是,如果阻塞应用程序的时间过长,Android会向用户提示 一些信息,即打开一个“应用程序没有相应(application not responding)” 的对话框。

② 现时android平台上实现gps获取经纬度有什么好方法

GPS获取经纬度可以参考以下方法:

  1. manifest中添加权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

2. 实例化一个locationmanager:

LocationManger
locationmanager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

3. 判断GPS是否打开,未打开提示打开GPS:

if (!locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();

return;

}

4. 监听位置信息变化:

private LocationListener locationListener=new LocationListener() {

/**

* 位置信息变化时触发

*/

public void onLocationChanged(Location location) {

updateView(location);

Log.i(TAG, "时间:"+location.getTime());

Log.i(TAG, "经度:"+location.getLongitude());

Log.i(TAG, "纬度:"+location.getLatitude());

Log.i(TAG, "海拔:"+location.getAltitude());

}

5. 加载监听器:

locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
locationListener);

③ 安卓修改GPS经纬度

尝试使用这款软件吧!

《伪装GPS Fake GPS
location》是一款能伪造你GPS定位的软件。不想让朋友知道你的位置吗?或者在切克或者点评上发表你的文章时候,不想让别人知道你在哪不。用这个软件吧。可以伪造出你的GPS位置。明明你在上海,但软件可以帮助显示你在美国。

④ Android高德通过经纬度获取地理位置

首先导入依赖:
首次导入依赖的过程可能有点慢

然后在需要用到逆地理编码的类或弹窗或其他地方中实现 GeocodeSearch.OnGeocodeSearchListener
监听
例子:

并实现其两个方法:

通过经纬度逆地理编码得到位置核心编码

这样我们就可以实现通过经纬度得到一个位置信息了

福利赠送~~~

1、继承 OnGeocodeSearchListener 监听。
2、构造 GeocodeSearch 对象,并设置监听。

3、通过 GeocodeQuery(java.lang.String locationName, java.lang.String city) 设置查询参数,调用 GeocodeSearch 的 getFromLocationNameAsyn(GeocodeQuery geocodeQuery) 方法发起请求。

4、通过回调接口 onGeocodeSearched 解析返回的结果。

说明:

1)可以在回调中解析result,获取坐标信息。

2)返回结果成功或者失败的响应码。1000为成功,其他为失败(详细信息参见网站开发指南-实用工具-错误码对照表)

阅读全文

与android经纬度转换工具相关的资料

热点内容
进化算法和启发式算法的区别 浏览:600
android组件是什么 浏览:973
安卓手机微信怎么同步信息 浏览:181
小人pdf 浏览:806
我的世界服务器怎么造好看的建筑 浏览:307
兄弟连培训php多少钱 浏览:249
1523铺地面的算法 浏览:746
linux源码安装php环境 浏览:821
ping命令用法 浏览:717
日本海军pdf 浏览:469
哪个app有大脸特效 浏览:140
自己生活需要解压吗 浏览:946
考研究生算法 浏览:528
java撤销 浏览:442
学完单片机做点什么好 浏览:312
编译后运行不了 浏览:404
安卓如何设置手机键盘大小 浏览:847
室内程序员表白方式 浏览:694
全民去哪里找app 浏览:124
表格命令CAD 浏览:244