我尝试在jVectormap中设置标记。我从我的数据库中获取内容并将其写入隐藏的输入字段。以下列格式返回:
{latLng:[52.5200066,13.404954],name:'Berlin'},{latLng:[53.0792962,8.8016937],name:'Bremen'},{latLng:[49.8728253,8.6511929],name:'Darmstadt'},{latLng:[50.1109221,8.6821267],name:'Frankfurt'},{latLng:[53.5510846,9.9936818],name:'Hamburg'},{latLng:[54.3232927,10.1227652],name:'Kiel'},{latLng:[50.937531,6.9602786],name:'Köln'},{latLng:[48.30694,14.28583],name:'Linz'},{latLng:[48.1351253,11.5819806],name:'München'},{latLng:[53.6355022,11.4012499],name:'Schwerin'},{latLng:[48.7758459,9.1829321],name:'Stuttgart'},{latLng:[48.0689177,11.6212533],name:'Unterhaching'},,{latLng:[48.2081743,16.3738189],name:'Wien'},
如果我想读这个字段没有任何反应。如果我将此行复制并粘贴到javascript部分,一切都很好。
这是有效的:
<script>
$(function(){
var map,
markers = [
{latLng:[52.5200066,13.404954],name:'Berlin'},{latLng:[53.0792962,8.8016937],name:'Bremen'},{latLng:[49.8728253,8.6511929],name:'Darmstadt'},{latLng:[50.1109221,8.6821267],name:'Frankfurt'},{latLng:[53.5510846,9.9936818],name:'Hamburg'},{latLng:[54.3232927,10.1227652],name:'Kiel'},{latLng:[50.937531,6.9602786],name:'Köln'},{latLng:[48.30694,14.28583],name:'Linz'},{latLng:[48.1351253,11.5819806],name:'München'},{latLng:[53.6355022,11.4012499],name:'Schwerin'},{latLng:[48.7758459,9.1829321],name:'Stuttgart'},{latLng:[48.0689177,11.6212533],name:'Unterhaching'},,{latLng:[48.2081743,16.3738189],name:'Wien'},
],
这不起作用:
$(function(){
var map,
markers = [
document.getElementById("geodata").value;
],
我的问题在哪里? :(
答案 0 :(得分:0)
输入中的值是JSON字符串,而不是对象,因此您必须将其解析为对象:
$(function(){
var map,
markers = [
JSON.parse(document.getElementById("geodata").value);
],
答案 1 :(得分:0)
所以我改变了一切不同的时间,现在它几乎正在工作。
我现在有“div”而不是输入。我更改了我的php函数并返回正确的JSON-Formt(这是我第一次尝试时的错误)
以下是javascript代码:
function GET_PLACES(){
var result_numbers = document.getElementById("result_numbers").value -1;
var text = "";
var result = "";
for (i = 1; i < result_numbers + 2; i++) {
text = JSON.parse(document.getElementById("geodata"+i).innerHTML);
}
return text;
}
console.log(GET_PLACES());
alert(JSON.stringify(GET_PLACES()));
$(function(){
var map,
markers = [GET_PLACES()],
...
我遇到的最后一个问题是它只能从循环中重新找到最后找到的JSON。
更新......这是解决方案
function GET_PLACES(){
var result_numbers = document.getElementById("result_numbers").value -1;
var text = "";
var result = [];
for (i = 1; i < result_numbers + 2; i++) {
text = JSON.parse(document.getElementById("geodata"+i).innerHTML);
result.push(text);
}
return result;
}
console.log(GET_PLACES());
alert(JSON.stringify(GET_PLACES()));
$(function(){
var map,
markers = GET_PLACES(),