vue解析json数据并设置为标记lat,lng到点

我从API获取json数据,但无法解析返回的数据,找不到任何解决方案。

 return {
            mapName: this.name + "-map",markerCoordinates: [{
                latitude: 21.423229,longitude: -0.1921837
            },{
                latitude: 24.505874,longitude: -12.1838486
            },{
                latitude: 66.4998973,longitude: 22.202432
            }],

,来自API的响应是:

[{"idLocation":1,"id":2,"latitude":"32.1231","longitude":"-12,5552","created_at":null,"updated_at":null},

我想将json数据传递到纬度和经度。

感谢您的帮助。

heniyoumeng 回答:vue解析json数据并设置为标记lat,lng到点

在您的回复中,您可以做到这一点。

将markerCoordinates声明为数组,因此看起来像这样。 markerCoordinates:[]

this.markerCoordinates.push({
     latitude: 'latitude data',longitude: 'longitude data
})
,

感谢您的回答,但我收到此警告:

Vue warn: Error in mounted hook: "TypeError: Cannot read property 'latitude' of undefined"

这是我的代码:

<script>
    import CarMap from "./CarMap";
    import axios from 'axios';

    export default {
        components: {CarMap},name: 'CarMap',props: ['CarMap'],data: function () {
            return {
                mapName: this.name + "-map",markerCoordinates: [],map: null,bounds: null,markers: [],locations: null,}
        },created() {
            this.fetchData();
        },methods: {
            fetchData() {
                this.error = this.locations = null;
                this.loading = true;
                axios
                    .get('/api/locations')
                    .then(response => (this.locations = response))
            }
        },mounted: function () {
            this.fetchData();
            this.bounds = new google.maps.LatLngBounds();
            const element = document.getElementById(this.mapName)
            const mapCentre = this.markerCoordinates[0]
            const options = {
                center: new google.maps.LatLng(mapCentre.latitude,mapCentre.longitude)
            }

            this.map = new google.maps.Map(element,options,document.getElementById('mapName'));
            this.markerCoordinates.push({
                latitude: 'locations.latitude',longitude: 'location.longitude'
            });
            this.markerCoordinates.forEach((coord) => {
                const position = new google.maps.LatLng(coord.latitude,coord.longitude);
                const marker = new google.maps.Marker({
                    position,map: this.map
                });
                this.markers.push(marker)
                this.map.fitBounds(this.bounds.extend(position))
            });
        }
    };
</script>

我想通过位置表/ api中的坐标在地图上设置标记。

本文链接:https://www.f2er.com/3141265.html

大家都在问