使用JavaScript获取Google地图上显示的所有推文时遇到麻烦

trouble getting all the tweets shown on the google map using javascript

本文关键字:文时 麻烦 遇到 显示 获取 JavaScript Google 地图 使用      更新时间:2023-09-26

你好,我试图将一些推文放在谷歌地图上,首先使用js-placemaker从推文的文本中提取位置,这是雅虎的网络服务,用于从任何类型的数据中对文本进行地理定位。

问题是我在地图上放置了不同的标记,但我只收到来自 Twitter resutlts 回调的最后一条推文,并且在地图上的每个标记上都看到了相同的profile_img_url。我也无法让信息窗口工作。

    function codeAddress(){
    var geocoder = new google.maps.Geocoder();;
    var mapOptions = {
    center: new google.maps.LatLng(35.74651,-39.46289), 
    zoom: 2,
     mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    // added this 
    var bounds = new google.maps.LatLngBounds();
    // create the map
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            $.getJSON("http://search.twitter.com/search.json?q=%23euronews&rpp=10&include_entities=true&result_type=mixed&callback=?", function (data) {
    $.each(data.results, function (i, item) {
    var screen_name = item.screen_name;
    var contentString=screen_name;
    var img = item.profile_image_url;
    var text=item.text;
    var profile_img=item.profile_image_url;
    var url=(item.entities.urls.length > 0 ? item.entities.urls[0].url : '');
    //  var latitude,longitude;
    Placemaker.getPlaces(text,
    function(o){
    console.log(o);
    var latitude=o.match.place.centroid.latitude,
    longitude=o.match.place.centroid.longitude; 
    var myLatLng = new google.maps.LatLng(latitude, longitude);
    var marker = new google.maps.Marker({
      icon: img,
      title: screen_name,
      map: map,
      position: myLatLng
    });
    var infowindow = new google.maps.InfoWindow({
    content: contentString
        });
    google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
        });
    bounds.extend(myLatLng);

    });
    });
    // map.fitBounds(bounds);
    });
    }

与这个类似问题相同的根本问题。 可以通过在异步回调上适当使用函数闭包来解决。

信息窗口的问题是没有内容(screen_name和内容字符串未定义)。