您的位置:首页 > 其它

百度地图之调用URL api接口实现周边搜索及计算球面两点间的距离

2014-04-24 21:34 323 查看
1.通过关键字,中心点的经纬度,搜索半径来进行周边搜索,这个可以自己封装个类,主要是一个get请求,返回的数据既可以是json格式,也可以是xml格式,需要指定

2.封装一个HttpClientUtil类用于发送get或post请求,也可以在封装一层,在其他地方直接调用该方法,输入关键字,经纬度,搜索半径进行调用,这里使用了searchAround()这个方法名

3.需要用到的jar包下载地址:http://download.csdn.net/detail/u013310075/7247879

代码如下:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

@SuppressWarnings("deprecation")
public class HttpClientUtil {

/**
* post 获取 rest 资源
*
* @param url
* @param name_value_pair
* @return
* @throws IOException
*/
public static String doPost(String url, List<NameValuePair> nameValuePair) throws IOException {
String body = "{}";
@SuppressWarnings("resource")
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
HttpResponse response = httpClient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpClient.getConnectionManager().shutdown();
}
return body;
}

/**
* get 获取 rest 资源
*
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String doGet(String url) throws ClientProtocolException, IOException {
String body = "{}";
@SuppressWarnings("resource")
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpClient.getConnectionManager().shutdown();
}
return body;
}

public static String searchAround(String keyWord,double longitude,double latitude,long radius) throws ClientProtocolException, IOException {
String path = null;
try {
String keyWordEncoder = URLEncoder.encode(keyWord, "UTF-8");
//通过output指定返回值类型,可以是json也可以是xml
path = "http://api.map.baidu.com/place/search?query="+keyWordEncoder+"&location="+latitude+","+longitude+"&radius="+radius+"&output=json";
return doGet(path);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

}


2.封装一个BaiduMapUtil的类,用于计算球面两点间的距离,单位(米)

public class BaiduMapUtil {
/**
* 计算两点之间距离
* @param start
* @param end
* @return 米
*/
public static double getDistance(double longitude1,double latitude1, double longitude2,double latitude2){
double lat1= (Math.PI/180)*latitude1;
double lat2 = (Math.PI/180)*latitude2;

double lon1 = (Math.PI/180)*longitude1;
double lon2 = (Math.PI/180)*longitude2;

//地球半径
double R = 6371;

//两点间距离 km,如果想要米的话,结果*1000就可以了
double d =  Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;

return d*1000;
}
}
3.写一个测试类(HttpClientTest)进行测试,代码如下

public class HttpClientTest {
public static void main(String[] args) {
/**根据关键字及经纬度,搜索半径得到周边搜索的json数据**/
String baiduMapJson = null;
try {
baiduMapJson = HttpClientUtil.searchAround("银行",102.680412,25.030081,3000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(baiduMapJson);
/**根据经纬度获得两点间的距离**/
double distance = BaiduMapUtil.getDistance(102.680412,25.030081, 102.681919,25.023);
System.out.println(distance);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: