微信小程序 当前所在城市定位

简介: function AMapWX(a) { this.key = a.key; this.requestConfig = { key: a.key, s: "rsx", platform: "WXJS", appname: a.key, sdkversion: "1.2.0", logversion: "2.0" }; this.MeRequestConfig = { key: a.key, serviceName: "https://restapi.amap.com/rest/me" } } AMapWX.prototyp

function AMapWX(a) {
this.key = a.key;
this.requestConfig = {
key: a.key,
s: "rsx",
platform: "WXJS",
appname: a.key,
sdkversion: "1.2.0",
logversion: "2.0"
};
this.MeRequestConfig = {
key: a.key,
serviceName: "https://restapi.amap.com/rest/me"
}
}
AMapWX.prototype.getWxLocation = function(a, b) {
wx.getLocation({
type: "gcj02",
success: function(c) {
c = c.longitude + "," + c.latitude;
wx.setStorage({
key: "userLocation",
data: c
});
b(c)
},
fail: function(c) {
wx.getStorage({
key: "userLocation",
success: function(d) {
d.data && b(d.data)
}
});
a.fail({
errCode: "0",
errMsg: c.errMsg || ""
})
}
})
};
AMapWX.prototype.getMEKeywordsSearch = function(a) {
if (!a.options) return a.fail({
errCode: "0",
errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
});
var b = a.options,
c = this.MeRequestConfig,
d = {
key: c.key,
s: "rsx",
platform: "WXJS",
appname: a.key,
sdkversion: "1.2.0",
logversion: "2.0"
};
b.layerId && (d.layerId = b.layerId);
b.keywords && (d.keywords = b.keywords);
b.city && (d.city = b.city);
b.filter && (d.filter = b.filter);
b.sortrule && (d.sortrule = b.sortrule);
b.pageNum && (d.pageNum = b.pageNum);
b.pageSize && (d.pageSize = b.pageSize);
b.sig && (d.sig =
b.sig);
wx.request({
url: c.serviceName + "/cpoint/datasearch/local",
data: d,
method: "GET",
header: {
"content-type": "application/json"
},
success: function(e) {
(e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
errCode: "0",
errMsg: e
})
},
fail: function(e) {
a.fail({
errCode: "0",
errMsg: e.errMsg || ""
})
}
})
};
AMapWX.prototype.getMEIdSearch = function(a) {
if (!a.options) return a.fail({
errCode: "0",
errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
});
var b = a.options,
c = this.MeRequestConfig,
d = {
key: c.key,
s: "rsx",
platform: "WXJS",
appname: a.key,
sdkversion: "1.2.0",
logversion: "2.0"
};
b.layerId && (d.layerId = b.layerId);
b.id && (d.id = b.id);
b.sig && (d.sig = b.sig);
wx.request({
url: c.serviceName + "/cpoint/datasearch/id",
data: d,
method: "GET",
header: {
"content-type": "application/json"
},
success: function(e) {
(e = e.data) && e.status && "1" === e.status &&
0 === e.code ? a.success(e.data) : a.fail({
errCode: "0",
errMsg: e
})
},
fail: function(e) {
a.fail({
errCode: "0",
errMsg: e.errMsg || ""
})
}
})
};
AMapWX.prototype.getMEPolygonSearch = function(a) {
if (!a.options) return a.fail({
errCode: "0",
errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
});
var b = a.options,
c = this.MeRequestConfig,
d = {
key: c.key,
s: "rsx",
platform: "WXJS",
appname: a.key,
sdkversion: "1.2.0",
logversion: "2.0"
};
b.layerId && (d.layerId = b.layerId);
b.keywords && (d.keywords = b.keywords);
b.polygon && (d.polygon = b.polygon);
b.filter && (d.filter = b.filter);
b.sortrule && (d.sortrule = b.sortrule);
b.pageNum && (d.pageNum = b.pageNum);
b.pageSize && (d.pageSize = b.pageSize);
b.sig && (d.sig = b.sig);
wx.request({
url: c.serviceName + "/cpoint/datasearch/polygon",
data: d,
method: "GET",
header: {
"content-type": "application/json"
},
success: function(e) {
(e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
errCode: "0",
errMsg: e
})
},
fail: function(e) {
a.fail({
errCode: "0",
errMsg: e.errMsg || ""
})
}
})
};
AMapWX.prototype.getMEaroundSearch = function(a) {
if (!a.options) return a.fail({
errCode: "0",
errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
});
var b = a.options,
c = this.MeRequestConfig,
d = {
key: c.key,
s: "rsx",
platform: "WXJS",
appname: a.key,
sdkversion: "1.2.0",
logversion: "2.0"
};
b.layerId && (d.layerId = b.layerId);
b.keywords && (d.keywords = b.keywords);
b.center && (d.center = b.center);
b.radius && (d.radius = b.radius);
b.filter && (d.filter = b.filter);
b.sortrule && (d.sortrule = b.sortrule);
b.pageNum && (d.pageNum = b.pageNum);
b.pageSize &&
(d.pageSize = b.pageSize);
b.sig && (d.sig = b.sig);
wx.request({
url: c.serviceName + "/cpoint/datasearch/around",
data: d,
method: "GET",
header: {
"content-type": "application/json"
},
success: function(e) {
(e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
errCode: "0",
errMsg: e
})
},
fail: function(e) {
a.fail({
errCode: "0",
errMsg: e.errMsg || ""
})
}
})
};
AMapWX.prototype.getGeo = function(a) {
var b = this.requestConfig,
c = a.options;
b = {
key: this.key,
extensions: "all",
s: b.s,
platform: b.platform,
appname: this.key,
sdkversion: b.sdkversion,
logversion: b.logversion
};
c.address && (b.address = c.address);
c.city && (b.city = c.city);
c.batch && (b.batch = c.batch);
c.sig && (b.sig = c.sig);
wx.request({
url: "https://restapi.amap.com/v3/geocode/geo",
data: b,
method: "GET",
header: {
"content-type": "application/json"
},
success: function(d) {
(d = d.data) && d.status && "1" === d.status ? a.success(d) : a.fail({
errCode: "0",
errMsg: d
})
},
fail: function(d) {
a.fail({
errCode: "0",
errMsg: d.errMsg || ""
})
}
})
};
AMapWX.prototype.getRegeo = function(a) {
function b(d) {
var e = c.requestConfig;
wx.request({
url: "https://restapi.amap.com/v3/geocode/regeo",
data: {
key: c.key,
location: d,
extensions: "all",
s: e.s,
platform: e.platform,
appname: c.key,
sdkversion: e.sdkversion,
logversion: e.logversion
},
method: "GET",
header: {
"content-type": "application/json"
},
success: function(g) {
if (g.data.status && "1" == g.data.status) {
g = g.data.regeocode;
var h = g.addressComponent,
f = [],
k = g.roads[0].name + "\u9644\u8fd1",
m = d.split(",")[0],
n = d.split(",")[1];
if (g.pois &&
g.pois[0]) {
k = g.pois[0].name + "\u9644\u8fd1";
var l = g.pois[0].location;
l && (m = parseFloat(l.split(",")[0]), n = parseFloat(l.split(",")[1]))
}
h.provice && f.push(h.provice);
h.city && f.push(h.city);
h.district && f.push(h.district);
h.streetNumber && h.streetNumber.street && h.streetNumber.number ? (f.push(h
.streetNumber.street), f.push(h.streetNumber.number)) : f.push(g.roads[0]
.name);
f = f.join("");
a.success([{
iconPath: a.iconPath,
width: a.iconWidth,
height: a.iconHeight,
name: f,
desc: k,
longitude: m,
latitude: n,
id: 0,
regeocodeData: g
}])
} else a.fail({
errCode: g.data.infocode,
errMsg: g.data.info
})
},
fail: function(g) {
a.fail({
errCode: "0",
errMsg: g.errMsg || ""
})
}
})
}
var c = this;
a.location ? b(a.location) : c.getWxLocation(a, function(d) {
b(d)
})
};
AMapWX.prototype.getWeather = function(a) {
function b(g) {
var h = "base";
a.type && "forecast" == a.type && (h = "all");
wx.request({
url: "https://restapi.amap.com/v3/weather/weatherInfo",
data: {
key: d.key,
city: g,
extensions: h,
s: e.s,
platform: e.platform,
appname: d.key,
sdkversion: e.sdkversion,
logversion: e.logversion
},
method: "GET",
header: {
"content-type": "application/json"
},
success: function(f) {
if (f.data.status && "1" == f.data.status)
if (f.data.lives) {
if ((f = f.data.lives) && 0 < f.length) {
f = f[0];
var k = {
city: {
text: "\u57ce\u5e02",
data: f.city
},
weather: {
text: "\u5929\u6c14",
data: f.weather
},
temperature: {
text: "\u6e29\u5ea6",
data: f.temperature
},
winddirection: {
text: "\u98ce\u5411",
data: f.winddirection + "\u98ce"
},
windpower: {
text: "\u98ce\u529b",
data: f.windpower + "\u7ea7"
},
humidity: {
text: "\u6e7f\u5ea6",
data: f.humidity + "%"
}
};
k.liveData = f;
a.success(k)
}
} else f.data.forecasts && f.data.forecasts[0] && a.success({
forecast: f.data.forecasts[0]
});
else a.fail({
errCode: f.data.infocode,
errMsg: f.data.info
})
},
fail: function(f) {
a.fail({
errCode: "0",
errMsg: f.errMsg || ""
})
}
})
}

    function c(g) {
        wx.request({
            url: "https://restapi.amap.com/v3/geocode/regeo",
            data: {
                key: d.key,
                location: g,
                extensions: "all",
                s: e.s,
                platform: e.platform,
                appname: d.key,
                sdkversion: e.sdkversion,
                logversion: e.logversion
            },
            method: "GET",
            header: {
                "content-type": "application/json"
            },
            success: function(h) {
                if (h.data.status && "1" == h.data.status) {
                    h = h.data.regeocode;
                    if (h.addressComponent) var f = h.addressComponent.adcode;
                    else h.aois && 0 < h.aois.length && (f = h.aois[0].adcode);
                    b(f)
                } else a.fail({
                    errCode: h.data.infocode,
                    errMsg: h.data.info
                })
            },
            fail: function(h) {
                a.fail({
                    errCode: "0",
                    errMsg: h.errMsg || ""
                })
            }
        })
    }
    var d = this,
        e = d.requestConfig;
    a.city ? b(a.city) : d.getWxLocation(a, function(g) {
        c(g)
    })
};
AMapWX.prototype.getPoiAround = function(a) {
    function b(e) {
        e = {
            key: c.key,
            location: e,
            s: d.s,
            platform: d.platform,
            appname: c.key,
            sdkversion: d.sdkversion,
            logversion: d.logversion
        };
        a.querytypes && (e.types = a.querytypes);
        a.querykeywords && (e.keywords = a.querykeywords);
        wx.request({
            url: "https://restapi.amap.com/v3/place/around",
            data: e,
            method: "GET",
            header: {
                "content-type": "application/json"
            },
            success: function(g) {
                if (g.data.status && "1" == g.data.status) {
                    if ((g = g.data) && g.pois) {
                        for (var h = [], f = 0; f < g.pois.length; f++) {
                            var k = 0 ==
                                f ? a.iconPathSelected : a.iconPath;
                            h.push({
                                latitude: parseFloat(g.pois[f].location.split(",")[1]),
                                longitude: parseFloat(g.pois[f].location.split(",")[0]),
                                iconPath: k,
                                width: 22,
                                height: 32,
                                id: f,
                                name: g.pois[f].name,
                                address: g.pois[f].address
                            })
                        }
                        a.success({
                            markers: h,
                            poisData: g.pois
                        })
                    }
                } else a.fail({
                    errCode: g.data.infocode,
                    errMsg: g.data.info
                })
            },
            fail: function(g) {
                a.fail({
                    errCode: "0",
                    errMsg: g.errMsg || ""
                })
            }
        })
    }
    var c = this,
        d = c.requestConfig;
    a.location ? b(a.location) : c.getWxLocation(a, function(e) {
        b(e)
    })
};
AMapWX.prototype.getStaticmap = function(a) {
    function b(e) {
        c.push("location=" + e);
        a.zoom && c.push("zoom=" + a.zoom);
        a.size && c.push("size=" + a.size);
        a.scale && c.push("scale=" + a.scale);
        a.markers && c.push("markers=" + a.markers);
        a.labels && c.push("labels=" + a.labels);
        a.paths && c.push("paths=" + a.paths);
        a.traffic && c.push("traffic=" + a.traffic);
        e = "https://restapi.amap.com/v3/staticmap?" + c.join("&");
        a.success({
            url: e
        })
    }
    var c = [];
    c.push("key=" + this.key);
    var d = this.requestConfig;
    c.push("s=" + d.s);
    c.push("platform=" + d.platform);
    c.push("appname=" + d.appname);
    c.push("sdkversion=" + d.sdkversion);
    c.push("logversion=" + d.logversion);
    a.location ? b(a.location) : this.getWxLocation(a, function(e) {
        b(e)
    })
};
AMapWX.prototype.getInputtips = function(a) {
    var b = Object.assign({}, this.requestConfig);
    a.location && (b.location = a.location);
    a.keywords && (b.keywords = a.keywords);
    a.type && (b.type = a.type);
    a.city && (b.city = a.city);
    a.citylimit && (b.citylimit = a.citylimit);
    wx.request({
        url: "https://restapi.amap.com/v3/assistant/inputtips",
        data: b,
        method: "GET",
        header: {
            "content-type": "application/json"
        },
        success: function(c) {
            c && c.data && c.data.tips && a.success({
                tips: c.data.tips
            })
        },
        fail: function(c) {
            a.fail({
                errCode: "0",
                errMsg: c.errMsg ||
                    ""
            })
        }
    })
};
AMapWX.prototype.getDrivingRoute = function(a) {
    var b = Object.assign({}, this.requestConfig);
    a.origin && (b.origin = a.origin);
    a.destination && (b.destination = a.destination);
    a.strategy && (b.strategy = a.strategy);
    a.waypoints && (b.waypoints = a.waypoints);
    a.avoidpolygons && (b.avoidpolygons = a.avoidpolygons);
    a.avoidroad && (b.avoidroad = a.avoidroad);
    wx.request({
        url: "https://restapi.amap.com/v3/direction/driving",
        data: b,
        method: "GET",
        header: {
            "content-type": "application/json"
        },
        success: function(c) {
            c && c.data && c.data.route && a.success({
                paths: c.data.route.paths,
                taxi_cost: c.data.route.taxi_cost || ""
            })
        },
        fail: function(c) {
            a.fail({
                errCode: "0",
                errMsg: c.errMsg || ""
            })
        }
    })
};
AMapWX.prototype.getWalkingRoute = function(a) {
    var b = Object.assign({}, this.requestConfig);
    a.origin && (b.origin = a.origin);
    a.destination && (b.destination = a.destination);
    wx.request({
        url: "https://restapi.amap.com/v3/direction/walking",
        data: b,
        method: "GET",
        header: {
            "content-type": "application/json"
        },
        success: function(c) {
            c && c.data && c.data.route && a.success({
                paths: c.data.route.paths
            })
        },
        fail: function(c) {
            a.fail({
                errCode: "0",
                errMsg: c.errMsg || ""
            })
        }
    })
};
AMapWX.prototype.getTransitRoute = function(a) {
    var b = Object.assign({}, this.requestConfig);
    a.origin && (b.origin = a.origin);
    a.destination && (b.destination = a.destination);
    a.strategy && (b.strategy = a.strategy);
    a.city && (b.city = a.city);
    a.cityd && (b.cityd = a.cityd);
    wx.request({
        url: "https://restapi.amap.com/v3/direction/transit/integrated",
        data: b,
        method: "GET",
        header: {
            "content-type": "application/json"
        },
        success: function(c) {
            c && c.data && c.data.route && (c = c.data.route, a.success({
                distance: c.distance || "",
                taxi_cost: c.taxi_cost ||
                    "",
                transits: c.transits
            }))
        },
        fail: function(c) {
            a.fail({
                errCode: "0",
                errMsg: c.errMsg || ""
            })
        }
    })
};
AMapWX.prototype.getRidingRoute = function(a) {
    var b = Object.assign({}, this.requestConfig);
    a.origin && (b.origin = a.origin);
    a.destination && (b.destination = a.destination);
    wx.request({
        url: "https://restapi.amap.com/v3/direction/riding",
        data: b,
        method: "GET",
        header: {
            "content-type": "application/json"
        },
        success: function(c) {
            c && c.data && c.data.route && a.success({
                paths: c.data.route.paths
            })
        },
        fail: function(c) {
            a.fail({
                errCode: "0",
                errMsg: c.errMsg || ""
            })
        }
    })
};
module.exports.AMapWX = AMapWX;

//onload
onLoad(options) {
this.nickName = options.username
// this.oldnickName = this.$queue.getData('nickName');
let that = this
var myAmapFun = new AMapWX({
key: '14e9b2cbba9e51ed8a3c14d2af1eb83e'
});
uni.getLocation({
success(res) {
//wx.setStorageSync('getLocation', res);
//获取到经纬度后调用高德逆地理API
myAmapFun.getRegeo({
location: ${res.longitude},${res.latitude},
success(data) {
console.log(data);
let str = {
province: '',
city: ''
}
// 拿到省份
str.province = data[0].regeocodeData.addressComponent.province;
// 拿到城市
str.city = data[0].regeocodeData.addressComponent.city;
if (Object.prototype.toString.call(str.city) == "[object Array]") {
str.city = str.city.join('');
}
//打印得到所有地址信息
console.log('da拿到城市ta[0]', data[0].regeocodeData.addressComponent.province)
that.city = data[0].regeocodeData.addressComponent.province
},
fail(err) {
console.log('333333333333333', err)
}
})
}
})

    },
相关文章
|
22天前
|
运维 监控 安全
上线就炸?别让测试止步于发布!年底稳交付,右移才是关键
年底上线频发“全绿即崩”困局?测试右移不是救火,而是主动防控:通过灰度发布、用户视角监控、反馈闭环、线上复盘和能力升级,将质量保障延伸至生产环境。左移防缺陷,右移守体验——双管齐下,方保上线又快又稳。
|
SQL Java 数据库
Spring Boot 的事务控制及示例代码
Spring Boot 提供了简单易用的事务控制功能,方便开发者进行数据库操作时保证数据的一致性和完整性。本文将介绍 Spring Boot 事务控制的用法和应用场景,并提供丰富的例子。
653 2
|
1月前
|
存储 数据采集 API
在周末休市期间,通过API接口获取历史数据进行分析
周末休市时,可利用API获取历史数据进行策略回测与组合优化。StockTv等平台提供稳定数据支持,结合Pandas、Scikit-learn等工具,实现数据清洗、分析与可视化,并通过自动化脚本定期更新,提升研究效率。
|
3月前
|
搜索推荐 数据可视化 数据库
用Python轻松打造专业PPT:自动化生成演示文稿全攻略
本文介绍如何用Python的python-pptx库自动化生成PPT,涵盖环境搭建、文本、图片、图表插入,以及批量生成与模板应用技巧。通过代码高效创建格式统一、内容丰富的演示文稿,大幅提升职场效率,适合报告、教学等场景,让PPT制作从繁琐变为智能。
1893 1
|
6月前
|
数据采集 人工智能 JSON
手把手教你写一个图片爬虫
本文介绍了一个实用的百度图片爬虫工具,仅需百余行 Python 代码即可实现自动搜索与下载图片功能,适用于需要批量获取图片素材的场景。爬虫包含请求处理、JSON 解析、文件操作等关键技术,并详细解析了其设计与实现原理,适合学习与扩展。
|
SQL Java 数据库连接
MyBatis实现动态SQL更新
1. 复杂一点,通过 MyBatis 的拦截器机制,在 SQL 拼接的 prepare 阶段修改 SQL 语句,实现动态 SQL。 考虑到拦截器机制不需要修改过多代码,因此本文博主将带领大家学习如何利用 MyBatis 拦截器机制来优雅的实现这个需求。
557 0
|
存储 缓存 定位技术
ArcGIS Pro栅格数据批量预处理工具
ArcGIS Pro栅格数据批量预处理工具
2482 0
|
Java UED
Java多线程实现文件上传详解
文件上传是Web应用程序中常见的功能之一,用户可以通过网页将文件从本地计算机上传到服务器。在处理大文件或多用户并发上传的情况下,为了提高性能和用户体验,常常使用多线程来实现文件上传功能。本文将详细介绍如何使用Java多线程实现文件上传,包括上传原理、多线程实现、代码示例等内容。
646 0
|
安全 前端开发 Java
Web端系统开发解决跨域问题——以Java SpringBoot框架配置Cors为例
在Web安全上下文中,源(Origin)是指一个URL的协议、域名和端口号的组合。这三个部分共同定义了资源的来源,浏览器会根据这些信息来判断两个资源是否属于同一源。例如,https://www.example.com:443和http://www.example.com虽然域名相同,但由于协议和端口号不同,它们被视为不同的源。同源(Same-Origin)是指两个URL的协议、域名和端口号完全相同。只有当这些条件都满足时,浏览器才认为这两个资源来自同一源,从而允许它们之间的交互操作。
488 0
Web端系统开发解决跨域问题——以Java SpringBoot框架配置Cors为例
|
开发工具
新人乘风者礼品兑换指南
仅限2023年11月15日(含11月15日)后入驻博主用于兑换礼品,此前完成入驻的博主按原邮寄方式进行。
4760 9

热门文章

最新文章