0

这是我的问题。我想将不同的图像设置为融合表给出的标准标记。我从包含我的点“(坐标,坐标)”的列中提取数据但是当我将此坐标与标记相关联时,这个坐标没有显示!我认为解决方案非常简单,但我无法得到它:(。请阅读此代码中间的“这是问题”部分以获得清晰的想法。谢谢!!!!

 function initialize() {
  geocoder = new google.maps.Geocoder();
  map = new google.maps.Map(document.getElementById('map_canvas'), {
    center: new google.maps.LatLng(46.06454, 13.23561), //the center lat and long
    zoom: 9, //zoom

    mapTypeId: google.maps.MapTypeId.ROADMAP //the map style
  });


  //make gviz request
  setData();

}

/* GVIZ - get data from Fusion Tables */

function setData() {
  //create a viz query to send to Fusion Tables
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + encodeURIComponent("SELECT dove FROM 781907"));

  //set the callback function that will be called when the query returns
  query.send(getData);

}

function getData(response) {

  numRows = response.getDataTable().getNumberOfRows();


  for (i = 0; i < numRows; i++) {

      var row = response.getDataTable().getValue(i,0);


    codeAddress(row);
  }
}


var geocoder;


function codeAddress(latlng) {

// HERE IS THE PROBLEM!!!!
// Test show correctly "(lat,lng)" BUT no marker showed on the map!
            document.getElementById("test").innerHTML = latlng; 
      geocoder.geocode( { 'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

        var marker = new google.maps.Marker({
          map: map, 
          position: latlng
//icon: new google.maps.MarkerImage("http://www.google.com/images/icons/product/fusion_tables-32.png")

      });

    } 
  });
}

更新:基于@Trott 的回答,可能是这样的?但仍然没有运行。有什么建议吗?

function getData(response) {

  numRows = response.getDataTable().getNumberOfRows();

  //create an array of row values
  for (i = 0; i < numRows; i++) {
    rowtext = response.getDataTable().getValue(i,0);
    var strArr[] = rowtext.split(",");
    document.getElementById("via").innerHTML = rowtext;
    row = new google.maps.LatLng(strArr[0],strArr[1]);
    codeAddress(row);
  }
}
4

1 回答 1

0

geocoder.geocode()需要一个LatLng对象。您的变量latlng只是文本。您需要找到一种将其转换为LatLng对象的方法。最明显的方法可能是用正则表达式或其他方式解析它,并将 lat 和 lng 传递给new google.maps.LatLng(). 可能有一个更优雅的解决方案,但这会奏效。

如果有帮助,这是我对您的代码进行的一些快速修改,以确认我在上面写的内容。我刚刚硬编码了你的第一对坐标。您仍然需要编写一些东西来解析数据。

function codeAddress(latlng) {
//Whoops, latlng is a string.  We need it to be an object.  Let's just hardcode one for demo purposes.
latlng=new google.maps.LatLng(46.0724339, 13.249490000000037);
geocoder.geocode( { 'latLng': latlng}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

        var marker = new google.maps.Marker({
          map: map,
          position: latlng,
          icon: new google.maps.MarkerImage("http://www.google.com/images/icons/product/fusion_tables-32.png")

      });

    }
  });
}

更新: 如果您(@Massimo)想按照您在更新中的方式进行操作,那么您需要删除括号和可能的空格。尝试更多类似的东西:

function getData(response) {

  numRows = response.getDataTable().getNumberOfRows();

  //create an array of row values
  for (i = 0; i < numRows; i++) {
    var rowtext = response.getDataTable().getValue(i,0);
    var sanitizedText = rowtext.replace(/[\(\)\s]/g, "");
    var strArr = sanitizedText.split(",");
    document.getElementById("via").innerHTML = rowtext;
    row = new google.maps.LatLng(strArr[0],strArr[1]);
    codeAddress(row);
  }
}
于 2011-05-23T16:24:41.073 回答