/** * * 由街道信息转换为经纬度 * @param address 街道信息 * @return 包含经纬度的一个double 数组,{longtitude,latitude} */ public static double[] getLocationInfoByGoogle(String address){ //定义一个HttpClient,用于向指定地址发送请求 HttpClient client = new DefaultHttpClient(); //向指定地址发送Get请求 HttpGet hhtpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+address+"ka&sensor=false"); StringBuilder sb = new StringBuilder(); try { //获取服务器响应 HttpResponse response = client.execute(hhtpGet); 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.getJSONObject("results").getJSONObject("0").getJSONObject("geometry").getJSONObject("location"); //获取经度信息 double longtitude = location.getDouble("lng"); double latitude = location.getDouble("lat"); return new double[]{longtitude,latitude}; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; }
/** * 根据经纬度获取对应地址,此处的经纬度须使用Google或者高德地图的经纬度;<br> * 若使用百度地图经纬度,须经过百度API接口(BMap.Convertor.transMore(points,2,callback))的转换; * @param longitude 经度 * @param latitude 纬度 * @return 详细街道地址 */ public static String getAddressByGoogle(double longitude,double latitude){ //定义一个HttpClient,用于向指定地址发送请求 HttpClient client = new DefaultHttpClient(); //向指定地址发送Get请求 HttpGet hhtpGet = 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(hhtpGet); 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.getJSONObject("results").getJSONObject("0").getJSONObject("formatted_address"); return location.toString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; }
/** * * 判断GPS是否开启,若未开启,则进入GPS设置页面;设置完成后需用户手动回界面 * @param currentActivity * @return */ public static void openGPSSettings(Context context){ //获取位置服务 LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //若GPS未开启 if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){ Toast.makeText(context, "请开启GPS!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.startActivity(intent); } } /** * * 判断GPS是否开启,若未开启,则进入GPS设置页面;设置完成后仍回原界面 * @param currentActivity * @return */ public static void openGPSSettings(Activity currentActivity){ //获取位置服务 LocationManager lm = (LocationManager) currentActivity.getSystemService(Context.LOCATION_SERVICE); //若GPS未开启 if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){ Toast.makeText(currentActivity, "请开启GPS!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); currentActivity.startActivityForResult(intent,0); //此为设置完成后返回到获取界面 } }
/** * 使用高德定位获取经纬度,包括GPS获取,网络获取; * * @param context 上下文环境 * @param locationListener 位置监听实例 * @return HashMap<String,Location> 返回Location实例的HashMap,其中,GPS对应的Location实例对应的Key值为"gps",网络为"network"; */ public static Map<String,Location> getLocationObject(Context context,LocationListener locationListener){ Map<String,Location> lMap = new HashMap<String, Location>(); LocationManagerProxy locationManager = LocationManagerProxy.getInstance(context); for(final String provider : locationManager.getProviders(true)){ //GPS if(LocationManagerProxy.GPS_PROVIDER.equals(provider) ) { Location l =locationManager.getLastKnownLocation(provider); if(null != l){ lMap.put(GPS, l); } locationManager.requestLocationUpdates(provider, mLocationUpdateMinTime, mLocationUpdateMinDistance,locationListener); break; } //网络 定位服务开启 if(LocationManagerProxy.NETWORK_PROVIDER.equals(provider)) { Location l =locationManager.getLastKnownLocation(provider); if(null != l){ lMap.put(NETWORK, l); } locationManager.requestLocationUpdates(provider, mLocationUpdateMinTime, mLocationUpdateMinDistance,locationListener); break; } } return lMap; }
第三步,解析地址:
/** * 使用高德地理解析,根据经纬度获取对应地址,;<br> * 若使用百度地图经纬度,须经过百度API接口(BMap.Convertor.transMore(points,2,callback))的转换; * @param context * @param longitude 经度 * @param latitude 纬度 * @return 详细街道地址 */ public static String getAddress(Context context,double longitude,double latitude){ String address= null; GeoPoint geo = new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); Geocoder mGeocoder = new Geocoder(context); int x = geo.getLatitudeE6();//得到geo 纬度,单位微度(度* 1E6) double x1 = ((double)x)/1000000; int y = geo.getLongitudeE6();//得到geo 经度,单位微度(度* 1E6) double y1 = ((double) y) / 1000000; //得到逆理编码,参数分别为:纬度,经度,最大结果集 try { //高德根据政府规定,在由GPS获取经纬度显示时,使用getFromRawGpsLocation()方法; List<Address> listAddress = mGeocoder.getFromRawGpsLocation(x1, y1, 3); if(listAddress.size()!=0){ Address a = listAddress.get(0); /* sb.append("getAddressLine(0)"+a.getAddressLine(0)+"\n"); sb.append("a.getAdminArea()"+a.getAdminArea()+"\n"); sb.append("a.getCountryName()"+a.getCountryName()+"\n"); sb.append("getFeatureName()"+a.getFeatureName()+"\n"); sb.append("a.getLocality()"+a.getLocality()+"\n"); sb.append("a.getMaxAddressLineIndex()"+a.getMaxAddressLineIndex()+"\n"); sb.append("getPhone()"+a.getPhone()+"\n"); sb.append("a.getPremises()"+a.getPremises()+"\n"); sb.append("a.getSubAdminArea()"+a.getSubAdminArea()+"\n"); sb.append("a.getSubLocality()"+a.getSubLocality()+"\n"); sb.append("getSubThoroughfare()"+a.getSubThoroughfare()+"\n"); sb.append("a.getThoroughfare()"+a.getThoroughfare()+"\n"); sb.append("a.getUrl()"+a.getUrl()+"\n"); */ address = a.getCountryName()+a.getLocality()+(a.getSubLocality()==null?"":a.getSubLocality())+(a.getThoroughfare()==null?"":a.getThoroughfare()) +(a.getSubThoroughfare()==null?"":a.getSubThoroughfare())+a.getFeatureName(); } } catch (AMapException e) { // TODO Auto-generated catch block e.printStackTrace(); } return address; }