1

I am iterating over twitter using python, the user inputs a topic to search for on twitter and then the location to view on the map in leaflet. The leaflet map is created automatically in the browser. How do I get the marker to show up at the tweet location assuming there is coordinates associated with the tweet which in a lot of cases does not happen. Below is the code I think you need to look into this. Suggestions? Using folium, webbrowser, os, geopy, TwitterSearch modules here.

for tweet in ts.search_tweets_iterable(tso):
        tweetText = ( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) )
        print tweetText
        if tweet['place'] is not None:
            (lat, lng) = geo(tweet['place']['full_name'])
            print 'And they said it from (' + str(lat) +', ' +str(lng)+')'
        else:
            print "And their place wasn't specified..."
Here is the piece for the marker using the folium module

for tweetLocation in ts.search_tweets_iterable(tso):
    folium.Marker([lat, lng], popup='tweetText').add_to(mapLeafletPython)

I originally had the for tweetLocation loop in the for tweet in ts.search...(tso): part but it wouldn't work. Have them pulled apart doesn't help either.

Here is some documentation for folium:

http://folium.readthedocs.org/en/latest/quickstart.html

also (looking through this now...)

https://pypi.python.org/pypi/folium

Let me know if you have questions, I'd be happy to provide additional details.

1
for tweet in ts.search_tweets_iterable(tso):
        tweetText = ( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode("ascii", "ignore")
        print tweetText
        if tweet['place'] is not None:
            (lat, lng) = geo(tweet['place']['full_name'])
            tw = tweet['text']
            user= tweet['user']['screen_name']
            folium.Marker([lat, lng], popup= user + " said " + tw).add_to(mapLeafletPython)
            print( '@%s tweeted: %s' % (user, tw) + 'And they said it from (' + str(lat) +', ' +str(lng)+')').encode("ascii", "ignore")
        else:
            print "And their place wasn't specified..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.