js API 高德開放平臺
阿新 • • 發佈:2018-12-13
平臺介紹
- 高德是阿里旗下的偏向地面服務的一個開放服務平臺,提供了眾多穩定、質量高、資料結構清晰明瞭的開放api;
- 開放api介面涵蓋web應用、移動端、pc端各種語言以及微信小程式等的資料介面;
- 介面面向個人開發者和企業開發者,申請方便!
解決方案
-
行業解決方案: 出行 O2O 電商 社交 運動 遊戲 智慧硬體 貨運
-
提供自定義地圖、資料視覺化和資料分析管理控制檯。
詳細的開發者文件
開發者文件以ip定位為例
- IP定位
最後更新時間: 2018年06月08日
- 產品介紹
IP定位是一個簡單的HTTP介面,根據使用者輸入的IP地址,能夠快速的幫使用者定位IP的所在位置。
使用API前您需先申請Key。若無高德地圖API賬號需要先申請賬號。
- 適用場景
希望能夠將IP資訊轉換為地理位置資訊。
- 使用限制
服務呼叫量的限制請點選這裡查閱。
- 使用說明
第一步,申請”web服務 API”金鑰(Key);
第二步,拼接HTTP請求URL,第一步申請的Key需作為必填引數一同傳送;
第三步,接收HTTP請求返回的資料(JSON或XML格式),解析資料。
如無特殊宣告,介面的輸入引數和輸出資料編碼全部統一為UTF-8。
- IP定位
IP定位API服務地址:
https://restapi.amap.com/v3/ip?parameters parameters代表的引數包括必填引數和可選引數。所有引數均使用和號字元(&)進行分隔。下面的列表枚舉了這些引數及其使用規則。
使用平臺提供的介面
-
控制檯建立應用
當然,要先註冊一個賬戶,並且至少升級為個人實名認證使用者或者企業認證使用者
-
配置你需要的服務,生成對應的key
-
最後得到一個這樣的key值:
這個key就是ajax提交中用於驗證的那個key值了
-
自己封裝的一個ajax方法
/*
ajax 方法
options 配置資訊
*/
function ajax(options) {
options.type = /post/i.test(options.type) ? 'POST' : 'GET';
//console.log( options.type);
//非同步或者同步
options.async = options.async === false ? false : true;
/*console.log(options.async)*/
var xhr = new XMLHttpRequest();
var data = ''; //user=韓梅梅&age=18
for (var k in options.data) {
data += k + '=' + encodeURIComponent(options.data[k]) + '&';
}
//是get還是post
if (options.type === 'GET') {
if (!/\?/.test(options.url)) {
options.url += '?';
} else {
if (!/(&\s*)$/.test(options.url)) {
options.url += '&';
}
}
options.url += data + '_=' + new Date().getTime();
data = null;
}
xhr.open(options.type, options.url, options.async);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
xhr.onreadystatechange = function () {
if (this.readyState === 4) { //0 1 2 3 4
if (this.status > 199 && this.status < 300 || this.status === 304) {
options.success && options.success.call(this, strJsonCode(this.response));
} else {
options.error && options.error.call(this, this.status);
}
}
};
xhr.send(data);
}
//將字串打包成json資料,打包失敗,預設返回原字串
function strJsonCode(str) {
try {
return JSON.parse(str);
} catch (e) {
return str;
}
}
//時間打包函式
function formatterDateTime() {
var date = new Date()
var month = date.getMonth() + 1
var datetime = date.getFullYear() +
"" // "年"
+
(month >= 10 ? month : "0" + month) +
"" // "月"
+
(date.getDate() < 10 ? "0" + date.getDate() : date
.getDate()) +
"" +
(date.getHours() < 10 ? "0" + date.getHours() : date
.getHours()) +
"" +
(date.getMinutes() < 10 ? "0" + date.getMinutes() : date
.getMinutes()) +
"" +
(date.getSeconds() < 10 ? "0" + date.getSeconds() : date
.getSeconds());
return datetime;
}
- 根據引數資訊,將必要引數給予規定值:
ajax({
type: 'get',
url: 'https://restapi.amap.com/v3/ip?parameters',
dataType: 'json',
data: {
"key": "***********your key************",
"output": "JSON"
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("查詢失敗!");
},
success: function (result) {
console.log(result);
}
});
介面資料將被result引數接收。