Java 호출 BingAPI 경위도 조회
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>
json 을 되 돌려 주 는 fastJSon 을 추천 합 니 다.이 예 에 서 는 사용 되 지 않 습 니 다.
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 가 매번 반환 값 이 있 는 것 이 아니 라 주소 형식 이 정확 하면 반환 값 이 없 으 면 여러 번 호출 할 수 있 습 니 다.값 을 찾 을 때 까지 예 에서 두 번 순환 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.