代码为一个可直接使用的工具类
解析api返回json格式时,用了以下jart包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
推荐使用fastJson来解析返回json,本例中未使用
package com.listings.util;
import java.io.IOException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BingUtil {
private final Logger LOGGER = LoggerFactory.getLogger(BingUtil.class);
public static int indexCount=0;
public static JSONArray getLongitudeLatitude(String country,String state,String postalcode,String city,String address) throws ClientProtocolException, IOException{
// String country="US";
// String state="CO";
// String postalcode="81602";
// String city="Glenwood Springs";
// String address="214 Eighth St, STE 209.";
JSONArray coordinates=new JSONArray();
HttpClient client = new DefaultHttpClient();
String city2 = city.replaceAll(" ", "");
String lastStr=address.substring(address.length()-1, address.length());
String address2="";
if (lastStr.equals(".")) {
address2=address.substring(0, address.length()-1);
}else
address2=address;
address2=address2.replaceAll(" ", "%20");
String url ="http://dev.virtualearth.net/REST/v1/Locations/"+country+"/"+state+"/"+postalcode+"/"+city2+"/"+address2+"?o=json&key="+Settings.getInstance().getString("bing.key");
HttpGet httpGet = new HttpGet(url);
// String url="http://dev.virtualearth.net/REST/v1/Locations/"+"US"+"/"+"CO"+"/"+"81602"+"/"+"GlenwoodSprings"+"/"+"214%20Eighth%20St,%20STE%20209"+"?o=json&key="+Settings.getInstance().getString("bing.key");
System.out.println(url);
HttpResponse resp = client.execute(httpGet);
HttpEntity httpEntity = resp.getEntity();
String result = EntityUtils.toString(httpEntity);
System.out.println(result);
JSONObject jsonObject=new JSONObject().fromObject(result);
String statusCode= jsonObject.getString("statusCode");
if(statusCode.equals("200")){
JSONArray resourceSets=(JSONArray) jsonObject.get("resourceSets");
JSONObject resources=(JSONObject) resourceSets.get(0);
JSONArray resource=(JSONArray) resources.get("resources");
if(indexCount<2){
if(resource.size()==0){
indexCount++;
coordinates=getLongitudeLatitude(country,state,postalcode,city,address);
}else{
JSONObject point=(JSONObject) resource.get(0);
JSONObject object=new JSONObject().fromObject(point.getString("point"));
coordinates=(JSONArray) object.get("coordinates");
indexCount=0;
}
}else{
indexCount=0;
throw new IOException();
}
}
return coordinates;
}
}
Settings.getInstance().getString("bing.key")为申请的key请自行申请。
需要注意的的是微软的经纬度api不是每次都有返回值,如果地址格式正确,若没返回值可调用多次,直到取到值为止,例子中循环2次