欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue利用高德地圖API實現(xiàn)實時天氣

 更新時間:2023年12月27日 11:44:41   作者:快樂是Happy  
這篇文章主要為大家詳細介紹了Vue如何利用高德地圖API實現(xiàn)實時天氣,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

前言

閑來無事,利用摸魚時間實現(xiàn)實時天氣的小功能

目錄

  • 登錄 高德開放平臺控制臺,如果沒有開發(fā)者賬號,請 注冊開發(fā)者。
  • 創(chuàng)建 key,進入應用管理,創(chuàng)建新應用,新應用中添加 key,服務平臺選擇 Web端(JS API)。
  • 獲取 key 和密鑰
  • 獲取當前城市定位
  • 通過定位獲取城市名稱、區(qū)域編碼,查詢目標城市/區(qū)域的實時天氣狀況

效果圖

這里樣式我就不做處理了,地圖可以不用做展示,只需要拿到獲取到天氣的結果,結合自己的樣式展示就可以了,未來天氣可以結合echarts進行展示,頁面效果更佳

實現(xiàn)

1.登錄高德開放平臺控制臺

2.創(chuàng)建 key

這里應用名稱可以隨便?。▊€人建議功能名稱或者項目稱)

3.獲取 key 和密鑰

4.獲取當前城市定位

首先,先安裝依賴

npm install @amap/amap-jsapi-loader --save

或者

pnpm add @amap/amap-jsapi-loader --save

頁面使用時引入即可 import AMapLoader from "@amap/amap-jsapi-loader"

  /** 1.  調(diào)用AMapLoader.load方法,通過傳入一個對象作為參數(shù)來指定加載地圖時的配置信息。
   *  -   key: 申請好的Web端開發(fā)者Key,是必填項,用于授權您的應用程序使用高德地圖API。
   *  -   version: 指定要加載的JSAPI版本,不指定時默認為1.4.15。
   *  -   plugins: 需要使用的插件列表,如比例尺、縮放控件等。
   */
  function initMap() {
  AMapLoader.load({
    key: "申請好的Web端開發(fā)者Key", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
    version: "2.0", // 指定要加載的 JSAPI 的版本,缺省時默認為 1.4.15
    plugins: [
      "AMap.ToolBar",
      "AMap.Scale",
      "AMap.HawkEye",
      "AMap.MapType",
      "AMap.Geolocation",
      "AMap.Geocoder",
      "AMap.Weather",
      "AMap.CitySearch",
      "AMap.InfoWindow",
      "AMap.Marker",
      "AMap.Pixel",
    ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  })
    .then((AMap) => {
      map.value = new AMap.Map("container", {
        //設置地圖容器id
        resizeEnable: true,
        viewMode: "3D", //是否為3D地圖模式
        zoom: 10, //初始化地圖級別
        center: locationArr.value, //初始化地圖中心點位置
      });

      getGeolocation(AMap);
      getCitySearch(AMap, map.value);
    })
    .catch((e) => {
      console.log(e);
    });
}

// 瀏覽器定位
const getGeolocation = (AMap: any) => {
  const geolocation = new AMap.Geolocation({
    enableHighAccuracy: true, //是否使用高精度定位,默認:true
    timeout: 1000, //超過10秒后停止定位,默認:5s
    position: "RB", //定位按鈕的??课恢?
    offset: [10, 20], //定位按鈕與設置的??课恢玫钠屏?,默認:[10, 20]
    zoomToAccuracy: true, //定位成功后是否自動調(diào)整地圖視野到定位點
  });
  map.value.addControl(geolocation);
  geolocation.getCurrentPosition(function (status: string, result: any) {
    if (status == "complete") {
      onComplete(result);
    } else {
      onError(result);
    }
  });
};

// IP定位獲取當前城市信息
const getCitySearch = (AMap: any, map: any) => {
  const citySearch = new AMap.CitySearch();
  citySearch.getLocalCity(function (
    status: string,
    result: {
      city: string;
      info: string;
    }
  ) {
    if (status === "complete" && result.info === "OK") {
      console.log(
        "?? ~ file: map-container.vue:88 ~ getCitySearch ~ result:",
        result
      );
      // 查詢成功,result即為當前所在城市信息
      getWeather(AMap, map, result.city);
    }
  });
};


onMounted(() => {
  initMap();
});

5.通過定位獲取城市名稱、區(qū)域編碼,查詢目標城市/區(qū)域的實時天氣狀況

const getWeather = (AMap: any, map: any, city: string) => {
  const weather = new AMap.Weather();
  weather.getLive(
    city,
    function (
      err: any,
      data: {
        city: string;
        weather: string;
        temperature: string;
        windDirection: string;
        windPower: string;
        humidity: string;
        reportTime: string;
      }
    ) {
      if (!err) {
        const str = [];
        str.push("<h4 >實時天氣" + "</h4><hr>");
        str.push("<p>城市/區(qū):" + data.city + "</p>");
        str.push("<p>天氣:" + data.weather + "</p>");
        str.push("<p>溫度:" + data.temperature + "℃</p>");
        str.push("<p>風向:" + data.windDirection + "</p>");
        str.push("<p>風力:" + data.windPower + " 級</p>");
        str.push("<p>空氣濕度:" + data.humidity + "</p>");
        str.push("<p>發(fā)布時間:" + data.reportTime + "</p>");
        const marker = new AMap.Marker({
          map: map,
          position: map.getCenter(),
        });
        const infoWin = new AMap.InfoWindow({
          content:
            '<div class="info" style="position:inherit;margin-bottom:0;background:#ffffff90;padding:10px">' +
            str.join("") +
            '</div><div class="sharp"></div>',
          isCustom: true,
          offset: new AMap.Pixel(0, -37),
        });
        infoWin.open(map, marker.getPosition());
        marker.on("mouseover", function () {
          infoWin.open(map, marker.getPosition());
        });
      }
    }
  );
 }

完整代碼

 <template>
  <div id="container"></div>
</template>

<script setup lang="ts">
import AMapLoader from "@amap/amap-jsapi-loader";
import { ref, onMounted, watch, reactive } from "vue";
const props = defineProps({
  search: {
    type: String,
    default: "杭州市",
  },
});

const isFalse = ref(false);

const map = ref<any>(null);
let locationArr = ref<any>();
watch(
  () => props.search,
  (newValue) => {
    console.log("search", newValue);
    initMap();
  }
);
function initMap() {
  AMapLoader.load({
    key: "申請好的Web端開發(fā)者Key", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
    version: "2.0", // 指定要加載的 JSAPI 的版本,缺省時默認為 1.4.15
    plugins: [
      "AMap.ToolBar",
      "AMap.Scale",
      "AMap.HawkEye",
      "AMap.MapType",
      "AMap.Geolocation",
      "AMap.Geocoder",
      "AMap.Weather",
      "AMap.CitySearch",
      "AMap.InfoWindow",
      "AMap.Marker",
      "AMap.Pixel",
    ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  })
    .then((AMap) => {
      map.value = new AMap.Map("container", {
        //設置地圖容器id
        resizeEnable: true,
        viewMode: "3D", //是否為3D地圖模式
        zoom: 10, //初始化地圖級別
        center: locationArr.value, //初始化地圖中心點位置
      });

      getGeolocation(AMap);
      getCitySearch(AMap, map.value);
    })
    .catch((e) => {
      console.log(e);
    });
}

// 瀏覽器定位
const getGeolocation = (AMap: any) => {
  const geolocation = new AMap.Geolocation({
    enableHighAccuracy: true, //是否使用高精度定位,默認:true
    timeout: 1000, //超過10秒后停止定位,默認:5s
    position: "RB", //定位按鈕的??课恢?
    offset: [10, 20], //定位按鈕與設置的??课恢玫钠屏浚J:[10, 20]
    zoomToAccuracy: true, //定位成功后是否自動調(diào)整地圖視野到定位點
  });
  map.value.addControl(geolocation);
  geolocation.getCurrentPosition(function (status: string, result: any) {
    if (status == "complete") {
      onComplete(result);
    } else {
      onError(result);
    }
  });
};

// IP定位獲取當前城市信息
const getCitySearch = (AMap: any, map: any) => {
  const citySearch = new AMap.CitySearch();
  citySearch.getLocalCity(function (
    status: string,
    result: {
      city: string;
      info: string;
    }
  ) {
    if (status === "complete" && result.info === "OK") {
      console.log(
        "?? ~ file: map-container.vue:88 ~ getCitySearch ~ result:",
        result
      );
      // 查詢成功,result即為當前所在城市信息
      getWeather(AMap, map, result.city);
    }
  });
};

// 天氣
const getWeather = (AMap: any, map: any, city: string) => {
  const weather = new AMap.Weather();
  weather.getLive(
    city,
    function (
      err: any,
      data: {
        city: string;
        weather: string;
        temperature: string;
        windDirection: string;
        windPower: string;
        humidity: string;
        reportTime: string;
      }
    ) {
      console.log("?? ~ file: map-container.vue:96 ~ .then ~ data:", data);
      if (!err) {
        const str = [];
        str.push("<h4 >實時天氣" + "</h4><hr>");
        str.push("<p>城市/區(qū):" + data.city + "</p>");
        str.push("<p>天氣:" + data.weather + "</p>");
        str.push("<p>溫度:" + data.temperature + "℃</p>");
        str.push("<p>風向:" + data.windDirection + "</p>");
        str.push("<p>風力:" + data.windPower + " 級</p>");
        str.push("<p>空氣濕度:" + data.humidity + "</p>");
        str.push("<p>發(fā)布時間:" + data.reportTime + "</p>");
        const marker = new AMap.Marker({
          map: map,
          position: map.getCenter(),
        });
        const infoWin = new AMap.InfoWindow({
          content:
            '<div class="info" style="position:inherit;margin-bottom:0;background:#ffffff90;padding:10px">' +
            str.join("") +
            '</div><div class="sharp"></div>',
          isCustom: true,
          offset: new AMap.Pixel(0, -37),
        });
        infoWin.open(map, marker.getPosition());
        marker.on("mouseover", function () {
          infoWin.open(map, marker.getPosition());
        });
      }
    }
  );

  // 未來4天天氣預報
  weather.getForecast(
    city,
    function (err: any, data: { forecasts: string | any[] }) {
      console.log(
        "?? ~ file: map-container.vue:186 ~ getWeather ~ data:",
        data
      );

      if (err) {
        return;
      }
      var strs = [];
      for (var i = 0, dayWeather; i < data.forecasts.length; i++) {
        dayWeather = data.forecasts[i];
        strs.push(
          `<p>${dayWeather.date}&nbsp&nbsp${dayWeather.dayWeather}&nbsp&nbsp${dayWeather.nightTemp}~${dayWeather.dayTemp}℃</p><br />`
        );
      }
    }
  );
};

function onComplete(data: any) {
  console.log("?? ~ file: map-container.vue:107 ~ onComplete ~ data:", data);
  const lngLat = [data.position.lng, data.position.lat];
  locationArr.value = lngLat;
}

function onError(data: any) {
  console.log("?? ~ file: map-container.vue:113 ~ onError ~ data:", data);
  // 定位出錯
}

onMounted(() => {
  initMap();
});
</script>

<style scoped lang="less">
#container {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 100%;
}
</style>

到此這篇關于Vue利用高德地圖API實現(xiàn)實時天氣的文章就介紹到這了,更多相關Vue實時天氣內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 解決vue打包之后靜態(tài)資源圖片失效的問題

    解決vue打包之后靜態(tài)資源圖片失效的問題

    下面小編就為大家分享一篇解決vue打包之后靜態(tài)資源圖片失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 詳解vue 中 scoped 樣式作用域的規(guī)則

    詳解vue 中 scoped 樣式作用域的規(guī)則

    這篇文章主要介紹了vue 中 scoped 樣式作用域的規(guī)則,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Vue實現(xiàn)動態(tài)顯示textarea剩余字數(shù)

    Vue實現(xiàn)動態(tài)顯示textarea剩余字數(shù)

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)動態(tài)顯示textarea剩余文字數(shù)量,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • element-ui中按需引入的實現(xiàn)

    element-ui中按需引入的實現(xiàn)

    這篇文章主要介紹了element-ui中按需引入的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • vue3?hook自動導入原理及使用

    vue3?hook自動導入原理及使用

    最近學習了hooks,特地寫一篇文章加深一下印象,下面這篇文章主要給大家介紹了關于vue3?hook自動導入原理及使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • 教你一招解決vue頁面自適應布局

    教你一招解決vue頁面自適應布局

    在前端開發(fā)的時候經(jīng)常會遇到這樣的困惑,設計師給你的設計稿的尺寸和頁面的尺寸不一致,導致了頁面無法正常顯示,下面這篇文章主要給大家介紹了關于一招解決vue頁面自適應布局的相關資料,需要的朋友可以參考下
    2022-07-07
  • vue項目實現(xiàn)中英文切換的詳細步驟

    vue項目實現(xiàn)中英文切換的詳細步驟

    這篇文章主要給大家介紹了關于vue項目實現(xiàn)中英文切換的詳細步驟,項目中經(jīng)常有中英文切換的功能,接下來就簡單實現(xiàn)以下這個功能,文中通過代碼介紹的非常詳細,需要的朋友可以參考
    2023-11-11
  • 利用vue+turn.js實現(xiàn)翻書效果完整實例

    利用vue+turn.js實現(xiàn)翻書效果完整實例

    這篇文章主要給大家介紹了關于利用vue+turn.js實現(xiàn)翻書效果的相關資料,turn.js是使用了jquery書寫的,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • vue 系列——vue2-webpack2框架搭建踩坑之路

    vue 系列——vue2-webpack2框架搭建踩坑之路

    本文從零搭建vue項目,給大家分享了我的vue2-webpack2框架搭建踩坑之路,需要的朋友可以參考下
    2017-12-12
  • vant的Uploader?文件上傳,圖片數(shù)據(jù)回顯問題

    vant的Uploader?文件上傳,圖片數(shù)據(jù)回顯問題

    這篇文章主要介紹了vant的Uploader?文件上傳,圖片數(shù)據(jù)回顯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論