0

I have a question about my info window display. Actually when I just load the page and move around for a bit without searching in the window it seems all to work fine with displaying the news or displaying a no news message. When I start to search for places and than try to click on markers I am getting some weird news like starting with Donald Trump's title or just empty info window at first and after clicking I am getting the same news with Donald Trump title at the top. What shall I add, I mean what code to help you guys to help me. I am on the verge of completing this problem set assignments ( apart from the one invented by me but want to sort the given tasks first).

function addMarker(place)
{
//set longitude and latitude to Floats
var myLat =  parseFloat(place.latitude);
var myLng =  parseFloat(place.longitude);

//create an icon
var image = new google.maps.MarkerImage(
    'http://timvandevall.com/wp-content/uploads/2013/05/printable-treasure-map-for-kids-4.jpg', //url
    null, //position
    null, //offset
    null, 
    new google.maps.Size(150, 40) //set icon size
    )



//create a marker 
var marker = new google.maps.Marker
({
    position : { lat : myLat, lng : myLng},  //set the position of a marker
    title : place.place_name + ' ' + place.postal_code, //the title of marker, visible on hover
    map : map,  //specify map
    icon : image,  //set the icon of marker
    label : place.place_name //set the lable
});
markers.push(marker); //add marker
//getting the articles
var query = place.place_name;
var content = "<ul\n>";
var content1 = "";
var parameters = {

    geo : query
}

$.getJSON("articles.php",parameters)
.done(function(data,textStatus,jqXHR)
{
    if(data.length){//checking if there are any info returned
        for(var i = 0; i<data.length;i++)
        {
            content += "<li><a href =" + data[i].link + ">" + data[i].title + "</a></li>"; //if yes populate infowindow with links to news
        }
        content += "</ul>";
    }
    else
    {
        content = "No news today!!"; //if no give user a feedback
    }

})
.fail(function(jqXHR, textStatus, errorThrown)
{
    console.log("ERROR IS :" + errorThrown.toString());
})

//display the articles while marker is clicked
marker.addListener('click',function(){
    showInfo(marker,content);
})
}

Search.php

require(__DIR__ . "/../includes/config.php");

// numerically indexed array of places
$places = [];

// TODO: search database for places matching $_GET["geo"], store in $places
$places = CS50::query("SELECT * FROM places WHERE MATCH(postal_code,place_name,admin_name1) AGAINST (?)",$_GET['geo']);

2 Answers 2

0

Yesterday when I tried to help you with the other question I was having this exact problem you're describing with my own code, which is (as far as I know) correct.

The problem seemed to be with the lookup function trying to reach google for local news, not succeeding, and then defaulting to query news from "the onion".

This is the part I'm talking about, inside helpers.php:

// download RSS from Google News if possible
$contents = @file_get_contents("http://news.google.com/news?geo=" . urlencode($geo) . "&output=rss", false, $context);
if ($contents !== false)
{
    // parse RSS
    $rss = @simplexml_load_string($contents);
    if ($rss !== false)
    {
        // iterate over items in channel
        foreach ($rss->channel->item as $item)
        {
            // add article to array
            $articles[] = [
                "link" => (string) $item->link,
                "title" => (string) $item->title
            ];
        }

        // cache articles
        if (!isset($_SESSION["cache"]))
        {
            $_SESSION["cache"] = [];
        }
        $_SESSION["cache"][$_GET["geo"]] = [
            "articles" => $articles,
            "timestamp" => time()
        ];
    }
}

// else from the Onion
else
{
    $contents = @file_get_contents("http://www.theonion.com/feeds/rss", false, $context);
    if ($contents !== false)
    {
        // parse RSS
        $rss = @simplexml_load_string($contents);
        if ($rss !== false)
        {
            // iterate over items in channel
            foreach ($rss->channel->item as $item)
            {
                // add article to array
                $articles[] = [
                    "link" => (string) $item->link,
                    "title" => (string) $item->title
                ];
            }
        }
    }
}

return $articles;

So I assumed the problem was on Google's side and not with my code. But let's wait for someone more knowledgeable to confirm my answer or give the correct one.

EDIT: A suggestion for not overloading google maps API:

Try to put this entire block:

//getting the articles
var query = place.place_name;
var content = "<ul\n>";
var content1 = "";
var parameters = {

    geo : query
}

$.getJSON("articles.php",parameters)
.done(function(data,textStatus,jqXHR)
{
    if(data.length){//checking if there are any info returned
        for(var i = 0; i<data.length;i++)
        {
            content += "<li><a href =" + data[i].link + ">" + data[i].title + "</a></li>"; //if yes populate infowindow with links to news
        }
        content += "</ul>";
    }
    else
    {
        content = "No news today!!"; //if no give user a feedback
    }

})
.fail(function(jqXHR, textStatus, errorThrown)
{
    console.log("ERROR IS :" + errorThrown.toString());
}) 

Inside the anonymous function of this block (right before showInfo(marker, content)):

//display the articles while marker is clicked
marker.addListener('click',function(){
    showInfo(marker,content);
})
4
  • I have added my addmarkers() just in case. I am also getting little problem with markers disappearing and stuff after I am getting issues with those callbacks hehe. I am hoping the code looks fine, I believe it should. Thanks for your interests again:) I am a real newbie to programming and I do enjoy it. Just have so much to learn yet. And I will check on it tomorrow as well haha:) Also attaching my search just in case if I messed something up in there, it is really short .
    – Kamil Kug
    Commented Oct 27, 2016 at 21:37
  • I'm a newbie too. We're just trying to help each other out here. Let's wait for someone who knows their stuff! Commented Oct 27, 2016 at 21:48
  • Guys, do you still have the same problem? Cause I do... Either see the same infoWIndow, or it is blank...
    – Pawel
    Commented Nov 2, 2016 at 23:01
  • @KamilKug I have updated the answer, if you want to take a look. Commented Nov 3, 2016 at 12:54
0

I had the same problem in this problem set with Python. In the end, I found out that for some reason, Python could not find one of the required libraries (urllib.parse) in helpers.py, therefore it could not query Google for the news properly.

Restarting the cloud9 workstation solved the problem for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .