首页 > 代码库 > 基于百度地图的android地理位置获取

基于百度地图的android地理位置获取

通过手机获取GPS信息,然后把利用GPS经纬度查询相应的地理位置信息。

1、利用android自带API实现,经过测试基本没有结果。留作参考。关键代码如下:

	Geocoder geocoder=new Geocoder(GotoMyLocationActivity.this); 
//		        Geocoder geocoder = new Geocoder(this, Locale.CHINA);
		       
		        
		        try {
		                //Thread.sleep(2000);
		                places = geocoder.getFromLocation(latitude, longitude, 5);
		               // Thread.sleep(2000);
		                Toast.makeText(GotoMyLocationActivity.this, places.size()+"", Toast.LENGTH_LONG).show();
		                System.out.println(places.size()+""); 
		        } catch (Exception e) {  
		                e.printStackTrace();
		        }


 String placename = "";
			        if (places != null && places.size() > 0) {
			                // placename=((Address)places.get(0)).getLocality();
			                //一下的信息将会具体到某条街
			                //其中getAddressLine(0)表示国家,getAddressLine(1)表示精确到某个区,getAddressLine(2)表示精确到具体的街
			                placename = ((Address) places.get(0)).getAddressLine(0) + ", " + System.getProperty("line.separator")
			                                + ((Address) places.get(0)).getAddressLine(1) + ", "
			                                + ((Address) places.get(0)).getAddressLine(2);
			                Toast.makeText(GotoMyLocationActivity.this, placename, Toast.LENGTH_LONG).show();
			               
			        }else{
			        	 Toast.makeText(GotoMyLocationActivity.this, "无法获取地理信息", Toast.LENGTH_LONG).show();
			        }
				

2、利用百度地图API实现

关键代码:

	// GPS编码成地理位置
				// 初始化搜索模块,注册事件监听
				// 把gps信号转成百度地图坐标
						JWD jwd = GPSToBaiDu.getData(longitude+"",latitude+"");
				//LatLng ptCenter = new LatLng(latitude, longitude);
						ptCenter = new LatLng(Double.parseDouble(jwd.getY()), Double.parseDouble(jwd.getX()));	
				

mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));
				mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
					
					@Override
					public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
						if (result == null
								|| result.error != SearchResult.ERRORNO.NO_ERROR) {
							Toast.makeText(GotoMyLocationActivity.this, "抱歉,未能找到结果",
									Toast.LENGTH_LONG).show();
							return;
						}
						Toast.makeText(GotoMyLocationActivity.this,
								result.getAddress(), Toast.LENGTH_LONG).show();

						mAddr = result.getAddress();


3、利用web api实现

关键代码:

/**
	 * 
	 * @param latitude 纬度
	 * @param longitude	经度
	 * @return ReturnValue 返回地址相关信息
	 */
	public static ReturnValue geocoding(String latitude, String longitude) {
		
		//String l = "http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&location=39.983424,116.322987&output=json&pois=1";
		//ak mean the key.you should apply it from baidu.
		String key = "XXXXXXXXXXXXXXXXX";
		String url = "http://api.map.baidu.com/geocoder/v2/" +
				"?ak="+key+"&location="+latitude+","+longitude+"&output=json&pois=1";
		ReturnValue rv = httpRequest(url,"GET");
		if(null==rv)
			return null;
		return rv;
	}

demo下载地址:http://download.csdn.net/detail/u014600432/8184399


基于百度地图的android地理位置获取