错误:传入变量时<img/>属性的值无效

时间:2015-01-14 10:39:44

标签: javascript json maps

当尝试使用OneMap地图覆盖图缩放到esri基本地图上的某个点时,我遇到了一些问题。这是通过传递邮政编码得到坐标x和y的函数:

function zoomPostal(postalCode) {
 $.getJSON("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr" +
        "&searchVal=" + postalCode + "&otptFlds=SEARCHVAL,CATEGORY" +
        "&returnGeom=0&rset=1", function (data) {

     var x =  data.SearchResults[1].X;
     console.log(x);
     var y =  data.SearchResults[1].Y;
     console.log(y);
    var loc = new esri.geometry.Point({ 
        "x": x, 
        "y": y, 
        "spatialReference":'PROJCS["SVY21",GEOGCS["SVY21[WGS84]",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",28001.642],PARAMETER["False_Northing",38744.572],PARAMETER["Central_Meridian",103.8333333333333],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",1.366666666666667],UNIT["Meter",1.0]]' });           

    map.centerAndZoom(loc, 5);
        });

}

当我打印出x和y时,我得到的是23948.7465和47505.7986,这意味着它不是NaN或null。但是,使用这些代码,我收到错误消息:

Error: Invalid value for <image> attribute x="NaN" 
Error: Invalid value for <image> attribute y="NaN" 

奇怪的是,如果我将坐标x和y硬编码为&#39; x&#34;和&#34; y&#34;在loc变量中,它确实放大并完美地工作。我不确定为什么它会以这种方式表现出来。

有什么想法吗?提前谢谢。

修改

这是显示错误消息的部分:

function zoomPostal(postalCode) {
 $.getJSON("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr" +
        "&searchVal=" + postalCode + "&otptFlds=SEARCHVAL,CATEGORY" +
        "&returnGeom=0&rset=1", function (data) {

     var x =  data.SearchResults[1].X;
     console.log(x);
     var y =  data.SearchResults[1].Y;
     console.log(y);
    var loc = new esri.geometry.Point({ 
        "x": x, 
        "y": y, 
        "spatialReference":'PROJCS["SVY21",GEOGCS["SVY21[WGS84]",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",28001.642],PARAMETER["False_Northing",38744.572],PARAMETER["Central_Meridian",103.8333333333333],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",1.366666666666667],UNIT["Meter",1.0]]' });           
    console.log(loc.x);
    map.centerAndZoom(loc, 5);
        });

}

从这些代码中,当我打印出来时,变量x和y 以及 loc.x 我得到23948.7465和47505.7986但是错误消息在那里

然后,我尝试硬编码x和y ,方法是输入,如下所示:

 function zoomPostal(postalCode) {
 $.getJSON("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr" +
        "&searchVal=" + postalCode + "&otptFlds=SEARCHVAL,CATEGORY" +
        "&returnGeom=0&rset=1", function (data) {

     var x =  data.SearchResults[1].X;
     console.log(x);
     var y =  data.SearchResults[1].Y;
     console.log(y);
    var loc = new esri.geometry.Point({ 
        "x": 23948.7465, 
        "y": 47505.7986, 
        "spatialReference":'PROJCS["SVY21",GEOGCS["SVY21[WGS84]",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",28001.642],PARAMETER["False_Northing",38744.572],PARAMETER["Central_Meridian",103.8333333333333],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",1.366666666666667],UNIT["Meter",1.0]]' });           
    console.log(loc.x);
    map.centerAndZoom(loc, 5);
        });

}

使用这些代码,缩放功能完美无误。

1 个答案:

答案 0 :(得分:1)

这个怎么样......

function zoomPostal(postalCode) {
 $.getJSON("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr" +
        "&searchVal=" + postalCode + "&otptFlds=SEARCHVAL,CATEGORY" +
        "&returnGeom=0&rset=1", function (data) {

     var x =  data.SearchResults[1].X;
     console.log(x);
     var y =  data.SearchResults[1].Y;
     console.log(y);
     var initializer = { "x": x, "y": y, "spatialReference":'PROJCS["SVY21",GEOGCS["SVY21[WGS84]",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",28001.642],PARAMETER["False_Northing",38744.572],PARAMETER["Central_Meridian",103.8333333333333],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",1.366666666666667],UNIT["Meter",1.0]]' }
    var loc = new esri.geometry.Point(initializer);           
    console.log(loc.x);
    map.centerAndZoom(loc, 5);
        });
}