Javascript 将json元素添加到变量
我有一些麻烦。我想配置GoogleMaps并从json文件中添加点。我想将'lat'和'lng'添加到我的'features'变量中(循环'results'的每个json文件元素),但它不起作用 json文件Javascript 将json元素添加到变量,javascript,json,google-maps,Javascript,Json,Google Maps,我有一些麻烦。我想配置GoogleMaps并从json文件中添加点。我想将'lat'和'lng'添加到我的'features'变量中(循环'results'的每个json文件元素),但它不起作用 json文件 {"act":"success","totalResults":122,"results":[{"id":"1","lat":"50.162292","lng":"-5.089257"},{"id":"2","lat":"50.164001","lng":"-5.069998"},{"id
{"act":"success","totalResults":122,"results":[{"id":"1","lat":"50.162292","lng":"-5.089257"},{"id":"2","lat":"50.164001","lng":"-5.069998"},{"id":"3","lat":"50.106697","lng":"-5.549815"},{"id":"4","lat":"50.129349","lng":"-5.515587"},{"id":"5","lat":"50.129333","lng":"-5.515833"},{"id":"6","lat":"50.128429","lng":"-5.519022"},{"id":"7","lat":"50.186115","lng":"-5.421418"},{"id":"8","lat":"50.225258","lng":"-5.274623"},{"id":"9","lat":"50.261471","lng":"-5.067650"}]}
脚本代码
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('mapGoogle'), {
zoom: 7,
center: new google.maps.LatLng(51.509865, -0.118092),
mapTypeId: 'roadmap',
gestureHandling: 'greedy',
disableDefaultUI: true
});
var myjson;
$.getJSON("http://app.regain-app.com/rest/V1/points/get?lat=50.164001&lng=-5.069998&distance=26", function(json){
myjson = json;
console.log(myjson);
});
var icons = {
info: {
icon: 'http://www.picpng.com/uploads/small/Google_Map_Marker_Red_Peg_77453.Png'
}
};
var features = [
{
position: new google.maps.LatLng(50.162292, -5.089257),
type: 'info'
}, {
position: new google.maps.LatLng(50.164001, -5.069998),
type: 'info'
}, {
position: new google.maps.LatLng(50.106697, -5.549815),
type: 'info'
}
];
myjson.results.forEach(function(object){
});
// Create markers.
features.forEach(function(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: icons[feature.type].icon,
map: map
});
});
}
</script>
var映射;
函数initMap(){
map=new google.maps.map(document.getElementById('mapGoogle'){
缩放:7,
中心:新google.maps.LatLng(51.509865,-0.118092),
mapTypeId:“路线图”,
手势处理:'贪婪',
disableDefaultUI:true
});
var-myjson;
$.getJSON(“http://app.regain-app.com/rest/V1/points/get?lat=50.164001&lng=-5.069998&distance=26“,函数(json){
myjson=json;
log(myjson);
});
变量图标={
信息:{
图标:'http://www.picpng.com/uploads/small/Google_Map_Marker_Red_Peg_77453.Png'
}
};
变量特征=[
{
位置:新google.maps.LatLng(50.162292,-5.089257),
键入:“信息”
}, {
位置:新google.maps.LatLng(50.164001,-5.069998),
键入:“信息”
}, {
位置:新google.maps.LatLng(50.106697,-5.549815),
键入:“信息”
}
];
myjson.results.forEach(函数(对象){
});
//创建标记。
features.forEach(函数(feature){
var marker=new google.maps.marker({
位置:feature.position,
图标:图标[feature.type]。图标,
地图:地图
});
});
}
如有任何帮助,我将不胜感激。:) 这里有很多工作要做 首先,你的循环。 你的线圈放错地方了
var myjson;
$.getJSON("http://app.regain-app.com/rest/V1/points/get?lat=50.164001&lng=-5.069998&distance=26", function(json){
myjson = json;
console.log(myjson);
});
这是程序设置变量myjson的地方。但是,它只发生在回调中,因此您以后无法在代码中循环它们,因此请输入以下代码:
myjson.results.forEach(function(object){
});
并在回调之前声明features变量
现在在forEach循环中:
features.push({
position: new google.maps.LatLng(parseInt(object.lat), parseInt(object.lng)),
type: 'info'
})
推送将向数组中添加一个新对象,该对象在括号中定义。在循环结果的地方,您还没有写入任何内容。你能展示你的尝试吗?