0
db.json:

{
   "manholes":[
      {
         "id":0,
         "lat":37.55009275087953,
         "lng":127.05067540273716,
         "type":"point"
      },
      {
         "id":1,
         "lat":37.5501997640179,
         "lng":127.04793121391802,
         "type":"point"
      }
   ]
}

get a json:

const baseURI = 'http://localhost:3000/manholes'
      this.$axios.get(baseURI)
        .then((result) => {
          this.manholes = result.data
          console.log(result.data)
        })

I tried to get just values (lat, lng) in foreach. But I could not get values.

this.manholes.forEach(function (data) {

      // I would like to use values from json
      var latlng = new kakao.maps.LatLng((data.lat), (data.lng))
      // making markers
      var marker = new kakao.maps.Marker({
        position: latlng,
        map: map,
      })

      marker.setMap(map)

}

Also I tried this way. I used object.keys(). but it is hard to get

Object.keys(this.manholes).forEach(function (data) {
          // 마커를 생성합니다
          console.log(JSON.parse(data.lat))
          console.log(JSON.parse(data.lng))

          var marker = new kakao.maps.Marker({
            position: new kakao.maps.LatLng(JSON.parse(data.lat), JSON.parse(data.lng)),
            map: map,
          })

code together: I got a json data and initialized a map when this paged is mounted

<script>

  export default {
    name: 'Monitoring',
    components: {

    },
    data: () => ({
      manholes: [],
    }),
    mounted () {
      const baseURI = 'http://localhost:3000/manholes'
      this.$axios.get(baseURI)
        .then((result) => {
          this.manholes = result.data
          console.log(this.manholes)
          // for (var item in result.data) {
          //   console.log(item, result.data[item])
          //   console.log(item, result.data[item].lat)
          //   console.log(item, result.data[item].lng)
          // }
        })

      window.kakao && window.kakao.maps
        ? this.initMap()
        : this.addKakaoMapScript()
    },
    methods: {
      initMap () {
        var container = document.getElementById('map') // 지도를 표실할 div
        var options = {
          center: new kakao.maps.LatLng(37.5500792286216, 127.0506923683668), // 지도의 중심좌표
          level: 3,
        }
        var map = new kakao.maps.Map(container, options) // 지도를 생성

        // var positions = [
        //   { title: '카카오', latlng: new kakao.maps.LatLng(37.55009275087953, 127.05067540273716) },
        //   { title: '생태연못', latlng: new kakao.maps.LatLng(37.5501997640179, 127.04793121391802) },
        // ]

        Object.keys(this.manholes).forEach(function (data) {
          // 마커를 생성합니다
          console.log(JSON.parse(data.lat))
          console.log(JSON.parse(data.lng))

          var marker = new kakao.maps.Marker({
            position: new kakao.maps.LatLng(JSON.parse(data.lat), JSON.parse(data.lng)),
            map: map,
          })
Jaeseo Lee
  • 172
  • 10
  • We need to see the code together so we can understand the relationship between the `axios` call and the `forEach()` call. – Randy Casburn Nov 04 '20 at 02:09
  • @RandyCasburn I added more code. I hope that it will help you understand code. – Jaeseo Lee Nov 04 '20 at 02:25
  • This should help you: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Randy Casburn Nov 04 '20 at 02:27
  • You are calling initMap too early. It executes before axios completes. The only place where code gets executed **after** axios completes is inside `.then()`. Everywhere else will execute before axios even makes the HTTP request – slebetman Nov 04 '20 at 03:40
  • I'm not sure what your problem is, but I notice `this.manholes = result.data` should be `this.manholes = result.data.manholes` – hungdoansy Nov 04 '20 at 09:48
  • @dshung1997 I got a data. but I would like to know how to access data such as lat, lng in json – Jaeseo Lee Nov 04 '20 at 10:25
  • @JaeseoLee Just do `result.data.manholes[0].lat`. `manholes` is an array, so you use forEach is correct – hungdoansy Nov 04 '20 at 13:09

0 Answers0