Geocoding java調用百度地圖API v2.0 圖文 實例( 解決102錯誤)


如何使用?

第一步:申請ak(即獲取密鑰),若無百度賬號則首先需要注冊百度賬號

第二步,拼寫發送http請求的url,注意需使用第一步申請的ak。

第三步,接收http請求返回的數據(支持json和xml格式)。

 

 

 

 

 這樣設置可以成功解決102的錯誤,即:HTTP連接后返回信息result={"status":102,"message":"IP\/SN\/SCODE\/REFERER Illegal:console-service"}

 

下面是一個小例子:

package tmp;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import com.infomorrow.tool.Parser_Tool;

/**
 * @author amosli
 * 
 */
public class badi_map_ {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        String ak = "orsN0xHVa8H0VxcMWW0uGf03";
        String url = "http://api.map.baidu.com/geocoder/v2/?address=上海五角場萬達廣場&output=json&ak=" + ak;
        //1.地理編碼服務,即根據地址來查經度緯度
        String return_value = Parser_Tool.do_get(url);
        System.out.println(return_value);
        // {"status":0,"result":{"location":{"lng":121.51996737545,"lat":31.308233584217},"precise":1,"confidence":80,"level":"\u5546\u52a1\u5927\u53a6"}}

//2.逆地理編碼,即根據經度緯度來查地址 String url2 = "http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&location=31.308233584217,121.51996737545&output=json&pois=1"; System.out.println(Parser_Tool.do_get(url2));
// {"status":0,"result":{"location":{"lng":121.51996737545,"lat":31.308233429954},"formatted_address":"上海市楊浦區政通路177","business":"五角場,大學區,復旦大學","addressComponent":{"city":"上海市","district":"楊浦區","province":"上海市","street":"政通路","street_number":"177"},"pois":[{"addr":"政通路177","cp":"NavInfo","distance":"0.022361","name":"萬達廣場C座","poiType":"辦公大廈,商務大廈","point":{"x":121.51996728562,"y":31.308233584217},"tel":"(021)61362818","uid":"18c7ec2a6c3b0e5e08ccddbe","zip":""},{"addr":"上海市楊浦區","cp":"NavInfo","distance":"76.894138","name":"麻辣風暴","poiType":"中餐館,餐飲","point":{"x":121.51999558224,"y":31.308826030169},"tel":"(021)55663716","uid":"e7b540bd7d31784b4d6d4670","zip":""},{"addr":"上海市楊浦區","cp":"NavInfo","distance":"80.359847","name":"H&M","poiType":"服裝鞋帽,購物","point":{"x":121.51968485837,"y":31.307663040014},"tel":"","uid":"badcec30996927a041ab9a26","zip":""},{"addr":"上海市楊浦區四平路2637號","cp":"mix","distance":"101.236603","name":"班尼路","poiType":"服裝鞋帽,購物","point":{"x":121.51911075132,"y":31.307971259271},"tel":"","uid":"802871ea45db67f7704b87bc","zip":""},{"addr":"上海市楊浦區","cp":"NavInfo","distance":"103.612502","name":"特力屋","poiType":"家居建材,購物","point":{"x":121.51957508543,"y":31.307508698607},"tel":"(021)52191919","uid":"bfd5bcd746f798e551e5c581","zip":""},{"addr":"上海市楊浦區","cp":"NavInfo","distance":"103.612502","name":"復茂","poiType":"中餐館,餐飲","point":{"x":121.51957508543,"y":31.307508698607},"tel":"","uid":"3c65d40795d0f74fefe4a523","zip":""},{"addr":"特力時尚匯5層","cp":"NavInfo","distance":"103.612502","name":"望湘園","poiType":"中餐館,餐飲","point":{"x":121.51957508543,"y":31.307508698607},"tel":"(021)33620977","uid":"8889b451bdeb6544e111033f","zip":""},{"addr":"上海市楊浦區翔殷路1128號大西洋百貨2樓(近國濟路)","cp":"mix","distance":"130.818973","name":"艾格","poiType":"服裝鞋帽,購物","point":{"x":121.5191145242,"y":31.307539242954},"tel":"","uid":"2d83695078e80ecf05fef54c","zip":""},{"addr":"上海市楊浦區","cp":"NavInfo","distance":"14.235832","name":"冠龍數碼彩擴沖印","poiType":"快照沖印,攝影沖印,生活服務","point":{"x":121.52008792805,"y":31.308270067458},"tel":"","uid":"80d3f20fca4819726849f393","zip":""},{"addr":"上海市楊浦區","cp":"NavInfo","distance":"55.990822","name":"美卡拉","poiType":"服裝鞋帽,購物","point":{"x":121.51972384483,"y":31.307855561471},"tel":"","uid":"f78e03f2963d1f271ee99941","zip":""}],"cityCode":289}} } }

 

 

 

這里用到了httpclient jar包,下載地址:http://hc.apache.org/downloads.cgi

/**
 * 
 */
package com.infomorrow.tool;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
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.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Parser_Tool {

    // /////////////////////////////http 請求/////////////////////////////////////
    /**
     * post 獲取 rest 資源
     * 
     * @param url
     * @param name_value_pair
     * @return
     * @throws IOException
     */
    public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httpost = new HttpPost(url);
            httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.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 do_get(String url) throws ClientProtocolException, IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        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;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM