javascript - 如何使用vue js通过ajax请求获取的json数据在Google map 上标记经纬度?

标签 javascript jquery vue.js vue-component vue-resource

这是我的 vue js 脚本,用于使用 ajax 请求获取 json 数据

    <script>
    new Vue({
  el: '#feed' ,
  data: {
    details: [],
  },
  mounted() {
    this.$nextTick(function() {
      var self = this;
      var id = window.location.href.split('=').pop()
             console.log(id)
      $.ajax({
            url: "https://",
            method: "GET",
            dataType: "JSON",
            success: function (e) {
                if (e.status == 1) {
                    self.details = e.data;
                    console.log(e.data)
                }
                else
                {
                    console.log('Error occurred');}
            }, error: function(){
            console.log('Error occurred');
            }
        });
    })
  },
})    </script>



 <script>
export default {
  name: 'google-map',
  props: ['name'],
  data: function () {
    return {
      mapName: this.name + "-map",
      markerCoordinates: [{
        latitude: 51.501527,
        longitude: -0.1921837
      }],
      map: null,
      bounds: null,
      markers: []
    }
  },
  mounted: function () {
    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);
    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>
<style scoped>
.google-map {
  width: 800px;
  height: 600px;
  margin: 0 auto;
  background: gray;
}
</style>

我收到错误 Uncaught SyntaxError: Unexpected token export.. 我不知道如何解决同样的问题。请帮我解决一下

这是我的 html 代码,用于显示所有 JSON 数据

<div class="m-single-article" id="feed">
<p>{{details.bussinessName}}</p> 
<p>{{details.pid}}</p>
<p v-for="inv in details.inventory">{{inv}}</p>
<p v-for="sub in details.sub_category">{{sub}}</p>
<div class="google-map" :id="mapName"></div>
</div>

但是我收到错误。谁能帮我解决我的问题。我怎样才能让纬度和经度显示在谷歌地图上。我是 vue js 的初学者。请帮我解决这个问题。

还有其他方法可以使用谷歌地图 API 解决相同问题吗?请帮助我找到解决方案 我的 js fiddle 链接没有添加谷歌地图是 https://jsfiddle.net/kzc26bgs/1/

最佳答案

function initMap() {
var c = [{"status": true, "data": {"pid": 5, "bussinessName": "Xavier Tailoring shop", "services": "All kind of stitching works", "inventory": [], "workHr": "Monday :9:00AM to 20:0PM,Thuesday :9:00AM to 20:0PM,Wednesday :9:00AM to 20:0PM,Tuesday : 9:00AM to 20:0PM,Friday :9:00AM to 20:0PM,Saturday :9:00AM to 20:0PM,Sunday :9:00AM to 20:0PM", "description": "All kind of stitching works", "category": 11, "sub_category": ["Veg Hotel"], "lat": 9.52436859, "lon": 76.82810117, "contactName": "xavier", "contactEmail": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cea6afbca7bdb6afb8a7abbc8ea9a3afa7a2e0ada1a3" rel="noreferrer noopener nofollow">[email protected]</a>", "contactOfficeAddress": "koovapally perubara road", "contactNumber": "8592808201", "contactOfficeNumber": "8592808201", "state": "Kerala", "city": "Koovappally", "place": "Kanjirapally - Erumely Road", "pincode": 686518, "referer": 24, "link": 31, "views": 0, "package": 1, "listing_pic": "default", "website": "example.com"}}];
  var myLatLng = {lat:c[0].data.lat  , lng:c[0].data.lon  };

  var map = new google.maps.Map(document.getElementById('mapName'), {
    zoom: 4,
    center: myLatLng
  });

  var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    title: 'Hello World!'
  });
}
<div id="mapName" style="width:267px; height: 270px" />
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AddKEY&callback=initMap">
</script>

var options = {
                    zoom: 6,
                    center: new google.maps.LatLng(12.92, 77.25), // Centered
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControl: false
                };

              // Init map
              var map = new google.maps.Map(document.getElementById('dvMap'), options);

 $.post("${pageContext.request.contextPath}/getorderlist.htm", {
                    aa : aa,
                    output : output,

                }, function(data) {             

                }).done(function(data) {
                    //alert(data);
                    var json = JSON.parse(data);


                        //console.log( json);

                    for (var i = 0; i < json.length; i++) 
                    {   

                         var    
                  png='http://maps.google.com/mapfiles/ms/icons/blue-dot.png';

                             // Init markers
                            var marker = new google.maps.Marker({
                                position: new google.maps.LatLng(json[i].lat , json[i].lon),
                                map: map,
                                title: 'Click Me ' + i,
                                icon:png
                            });

                            // Process multiple info windows
                            (function(marker, i) {
                                // add click event
                                google.maps.event.addListener(marker, 'click', function() {
                                    infowindow = new google.maps.InfoWindow({
                                        content: json[k][i].address
                                    });
                                    infowindow.open(map, marker);
                                });
                            })(marker, i);                              

                        }                           


                }).fail(function(xhr, textStatus, errorThrown) {
                }).complete(function() {
                    $("#btn-save").prop("disabled", false);

                });

关于javascript - 如何使用vue js通过ajax请求获取的json数据在Google map 上标记经纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171987/

相关文章:

javascript - 当满足条件时,从多个下拉列表中进行选择更新

javascript - 谷歌地图的 AngularJS 路由

javascript - 如何检测添加到 div 上的所有事件

jquery - CSS 变换比例属性

javascript - 何时在 javascript 中使用 'return'

javascript - 使用 jquery 或 Javascript 更改 RadTextbox(Telerik textbox) 和 RadDatePicker(Telerik DatePicker) 背景颜色

javascript - 使用 Chrome 扩展更改 DOM 内容

javascript - 实例v-show的问题-Nuxt js菜单

javascript - 在 Vue.js 中,组件可以检测插槽内容何时更改

html - vue.js 无数据渲染