有了经纬度坐标,就可以得到城市的:adcode区域编码
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
- <title>根据ip定位</title>
- <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
- <style type="text/css">
- html,body,#container{
- height:100%;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div class="info" id="text">
- 请用鼠标在地图上操作试试
- </div>
- <div class="input-card" style="width:16rem">
- <h4>地图点击相关事件</h4>
- <div>
- <div class="input-item">
- <button id="clickOn" class="btn" style="margin-right:1rem;">绑定事件</button>
- <button id="clickOff" class="btn">解绑事件</button>
- </div>
- </div>
- </div>
-
- <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=填入高德Web端key&plugin=AMap.CitySearch"></script>
- <script type="text/javascript">
- var map;
- var marker;
- var ilon = 114.468668;
- var ilat = 38.03703;
-
- var map = new AMap.Map("container", {
- zoom: 13,
- });
-
- // 实例化点标记
- marker = new AMap.Marker({
- icon: "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
- position: [ilon, ilat],
- offset: new AMap.Pixel(-25, -60)
- });
- map.setZoomAndCenter(13, [ilon, ilat]);
- map.add(marker)
-
- function showInfoDbClick(e){
- var text = '您在 [ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ] 的位置双击了地图!'
- document.querySelector("#text").innerText = text;
- }
- function showInfoMove(){
- var text = '您移动了您的鼠标!'
- document.querySelector("#text").innerText = text;
- }
- // 事件绑定
- function clickOn(){
- console.log('绑定成功')
- map.on('click', function (e) {
- map.clearMap();
- //console.log(e.lnglat);
- marker = new AMap.Marker({
- icon: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
- position: [e.lnglat.lng, e.lnglat.lat],
- offset: new AMap.Pixel(-25, -60)
- });
- var text = '您在 [ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ] 的位置单击了地图!'
- document.querySelector("#text").innerText = text;
- console.log(text)
- map.add(marker);
-
- })
- map.on('dblclick', showInfoDbClick);
- map.on('mousemove', showInfoMove);
-
- }
- // 解绑事件
- function clickOff(){
- log.success("解除事件绑定!");
- map.off('click', showInfoClick);
- map.off('dblclick', showInfoDbClick);
- map.off('mousemove', showInfoMove);
- }
-
- // 给按钮绑定事件
- document.getElementById("clickOn").onclick = clickOn;
-
- </script>
- </body>
- </html>
使用的是web服务API
请用鼠标在地图上操作试试
style="width:16rem">
省份
城市
天气
气温
风向
风力级别
空气湿度
数据发布时间
/* 卡片容器样式 */
.showweatherdata {
display: grid;
grid-template-columns: repeat(2, 1fr); /* 4列 */
grid-gap: 10px; /* 网格间距 */
padding: 20px; /* 内边距 */
border-radius: 10px; /* 圆角 */
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 轻微的阴影,模拟卡片效果 */
margin: 20px; /* 外边距 */
}/* 文本样式 */
.showweatherdata text {
font-size: 16px; /* 字体大小 */
color: #333; /* 字体颜色 */
line-height: 1.5; /* 行高 */
margin-bottom: 5px; /* 下边距 */
}/* 每行的最后一个元素不显示下边距 */
.showweatherdata text:last-child {
margin-bottom: 0;
}/* 为每一行添加额外的间距,实现 2 行布局 */
.showweatherdata text:nth-child(-n+8) {
margin-bottom: 20px; /* 行间距 */
}/* 绿色调主题颜色 */
.showweatherdata .province,
.showweatherdata .city,
.showweatherdata .weather,
.showweatherdata .temperature,
.showweatherdata .winddirection,
.showweatherdata .windpower,
.showweatherdata .humidity,
.showweatherdata .reporttime {
color: #2e7d32; /* 主题绿色 */
border-left: 4px solid #43a047; /* 边框颜色 */
padding-left: 10px; /* 内边距 */
}/* 媒体查询,适应小屏幕设备 */
@media (max-width: 768px) {
.showweatherdata {
grid-template-columns: repeat(2, 1fr); /* 在小屏幕上改为 2 列 */
}
}
.container {
position: relative;
height: 100vh;
display: flex;
flex-direction: column;
}#map-container {
flex: 1;
}.info {
padding: 10px;
text-align: center;
background-color: #f0f0f0;
}.input-card {
padding: 10px;
margin-top: 10px;
background-color: #f9f9f9;
border-radius: 5px;
}.title {
font-size: 16px;
font-weight: bold;
}.btn {
padding: 5px 10px;
background-color: #409eff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
这个的web端key是
import App from './App'
import uView from "@/uni_modules/uview-ui"
import AMapLoader from '@amap/amap-jsapi-loader'Vue.use(uView)
AMapLoader.load({
"key": "",
"version": "2.0"
}).then(() => {
new Vue({
render: h => h(App)
}).$mount('#app')
})
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
// #endif// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
// #endif