首页 > 其他分享 >根据地址查询经纬度

根据地址查询经纬度

时间:2023-04-06 21:32:25浏览次数:48  
标签:null return 经纬度 strSearchAddress 地址 address catch new 查询


有时候需要根据具体位置查询该地址对应的经纬度,然后将其保存到数据库中......

import android.location.Address;
import android.location.Geocoder;

/**
	 * 根据地址查询经纬度
	 * @param strSearchAddress 查询地址
	 * @return
	 */
	private Address getGeoByAddress(String strSearchAddress) {
		Address address=null;
		try {
			if (strSearchAddress != null&&strSearchAddress.length()>0) {
				Geocoder geocoder = new Geocoder(context,Locale.getDefault());
//				Geocoder geocoder = new Geocoder(context,Locale.US);
				List<Address> addresses = geocoder.getFromLocationName(strSearchAddress, 1);
				if (!addresses.isEmpty()) {
					address = addresses.get(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return address;
	}




不是每次都能获取到经纬度的,有时候会抛出unable to parse response from server 异常。


这是google map的bug,详情请见:


http://code.google.com/p/android/issues/detail?id=8816

当抛出异常后需要换一种方法解决,


直接请求url获得json数据,然后解析:


public 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();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject = new JSONObject(stringBuilder.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return jsonObject;
	}
	
	public Address getGeoPoint(JSONObject jsonObject) {
		if(jsonObject==null){
			return null;
		}
		Double lng = new Double(0);
		Double lat = new Double(0);
		try {
			lng = ((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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		Address address=new Address(Locale.US);
		address.setLatitude(lat);
		address.setLongitude(lng);
		return address;
	}



在getGeoByAddress()方法的try catch中起一个线程执行getLocationInfo()方法:


}catch (Exception e) {
    strSearchAddress=strSearchAddress.replace(" ", "+").replace(",", ",+");
     DownloadTask task = new DownloadTask(strSearchAddress);//注意同步问题,要先返回,后操作。
     task.execute();
}


最后使用的时候要注意线程同步!


.........
Address latLong=getGeoByAddress(detailAddress);
			try {
//休眠2秒,好让请求url时有足够的时间返回经纬度。虽然是个很馊的主意,但至少有效。
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if(latLong!=null){
				if(latLong.getLatitude()!=0&&latLong.getLongitude()!=0){
					App.store.setLatitude(latLong.getLatitude());//纬度
					App.store.setLongitude(latLong.getLongitude());//经度
				}
			}
.........



class DownloadTask extends AsyncTask<String, Integer, JSONObject> {
		String strSearchAddress;
		DownloadTask(String strSearchAddress) {
			this.strSearchAddress=strSearchAddress;
		}

		@Override
		protected JSONObject doInBackground(String... params) {
			JSONObject obj=getLocationInfo(strSearchAddress);
			return obj;
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			Address address=getGeoPoint(result);
			if(address!=null){//赋值
				if(address.getLatitude()!=0&&address.getLongitude()!=0){
					App.store.setLatitude(address.getLatitude());//纬度
					App.store.setLongitude(address.getLongitude());//经度
				}
			}
		}

		@Override
		protected void onPreExecute() {
			// 预处理
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// 更新进度
		}
	}





实现Android的不同精度的定位(基于网络和GPS) 


http://lszdb1983.blog.163.com/blog/static/2042634820126221635756/



android Location 过滤最佳的位置


http://lipeng88213.iteye.com/blog/1936338

标签:null,return,经纬度,strSearchAddress,地址,address,catch,new,查询
From: https://blog.51cto.com/u_5454003/6174179

相关文章

  • 支付宝网站支付即时到帐(一) 参数的文档地址
    必备物品:企业支付宝账号官网: https://open.alipay.com/platform/home.htm第一步 创建网站应用 并完善信息 这一步很简单添加上网站支付的功能第二步 找到即时到帐的文档https://docs.open.alipay.com/62/103566上面有获取pid的方法https://docs.open.alipay.com/common/10473......
  • 保证高效写入查询的情况下,如何实现 CPU 资源和磁盘 IO 的最低开销?
    从《写入性能:TDengine最高达到InfluxDB的10.3倍,TimeScaleDB的6.74倍》、《查询性能:TDengine最高达到了InfluxDB的37倍、TimescaleDB的28.6倍》两篇文章中,我们发现,TDengine不仅在写入和查询性能上超越了InfluxDB和TimescaleDB,在数据处理过程的资源消耗也比两......
  • js中通过数据库查询省,后自动带出市显示的问题
    //注册地址varprovince=SQL("selectprovincefromprojectwherecode='"+code+"'andisdelete=0orderbyindatelimit1");$("#field_2c923daf70a540290170aa8ad7a102ff").val(province);varcity=SQL("selectcityfrom......
  • jmeter模拟多IP地址访问
    1.前言:今天一同事在压测时提到怎么用jmeter里虚拟多个ip来发送请求,我想了一下以前用LR时用过虚拟ip地址,jmeter还没有使用过。想着原理应该是相通的,既然LR都能支持的话,那Jmeter应该也是支持,于是就有了jmeter虚拟化IP地址的研究。在网上也查找了相应的资料,摸索参考着实践了一把,坑吃......
  • jmeter模拟多IP地址访问
    1.前言:今天一同事在压测时提到怎么用jmeter里虚拟多个ip来发送请求,我想了一下以前用LR时用过虚拟ip地址,jmeter还没有使用过。想着原理应该是相通的,既然LR都能支持的话,那Jmeter应该也是支持,于是就有了jmeter虚拟化IP地址的研究。在网上也查找了相应的资料,摸索参考着实践了一把,坑吃......
  • es查询三种方式,Elasticsearch查询速度为什么这么快
    Elasticsearch查询速度为什么这么快?es查询三种方式 ......
  • 《操作系统原理、实现与实践》实践项目5&6:信号量&地址映射与共享
    教员布置的操作系统的第一个大作业,其中有一些知识相对而言比较重要,包括一些操作过程都具有重复实验的价值;所以写一个blog记录一下,方便一下以后重复这个大实验或是复现。分为三个部分:实验环境的配置、实验5的实现、实验6的实现。实验环境的配置:KylinOS应该是可以实现的,实现的原......
  • js用前缀名查找class或id节点,js模糊查询某个dom节点
    js在操作dom的场景中,有时候会有类似的场景需求。js用前缀名查找class节点//参数dom为htmldom节点//参数key为需模糊查询的名称字段functionqueryClassNode(dom,key){letcollectArray=[];for(vari=0;i<dom.childNodes.length;i++){ //核心点......
  • 基于SSM+JSP实现的流浪猫狗救助系统(分为用户端和管理员端,领养动物、流浪动物知识学习
    @目录基于SSM+JSP实现的流浪猫狗救助系统实现功能截图系统功能使用技术完整源码基于SSM+JSP实现的流浪猫狗救助系统本系统是一个流浪猫狗救助系统,分为用户端和管理员端,用户端可以完成登陆注册、流浪猫狗知识学习、领养动物,管理员端可以对用户管理、流浪猫狗管理、领养管理、评论......
  • easy-excel读取远程地址获得文件进行上传
    背景作为一个快五年的程序员,一直以来还没有自己维护过自己的技术栈,最近也是有时间,所以也是下定决心,从头开始,一步一步的夯基础。最近在系统化的学习easy-excel,今天遇到了一个问题,特意记录一下。目的尝试前端上传文件到文件存储器,然后请求的时候带上这个路径,后台解析这个路径,做ex......