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,
})