Search code examples
scalatwittertwitter4j

Location track items must be given as pairs of comma separated lat/longs Twitter4J Scala


I am trying to filter tweets by user locations. I am using Twitter4J in Scala for that. But every time I get an error

Parameter not accepted with the role. 406:Returned by the Search API when an invalid format is specified in the request.
Returned by the Streaming API when one or more of the parameters are not suitable for the resource. The track parameter, for example, would throw this error if:
The track keyword is too long or too short.
The bounding box specified is invalid.
No predicates defined for filtered resource, for example, neither track nor follow parameter defined.
Follow userid cannot be read.
Location track items must be given as pairs of comma separated lat/longs: [Ljava.lang.String;@48d1de0f

Here is my code:

def main(args: Array[String]): Unit = {    
    val twitterStream = TwitterStreamFilters.configureTwitterStream()
        val counter = new Counter
        twitterStream.addListener(counter)
        TwitterStreamFilters.filterTwitterStreamByLocation(twitterStream, Array(-122.75,36.8,-121.75,37.8))
        TwitterStreamFilters.closeTwitterStream(twitterStream)
    }

I took the location for San Francisco from this link. So why this twitter stream doesn't work? I would be grateful for any advice.

The method for locations:

  def filterTwitterStreamByLocation(twitterStream: TwitterStream, coordinates: Array[Double]) = {
    twitterStream.filter(new FilterQuery().locations(coordinates))
  }

Solution

  • So following my comment above, this should solve your issue:

    def main(args: Array[String]): Unit = {    
        val twitterStream = TwitterStreamFilters.configureTwitterStream()
            val counter = new Counter
            twitterStream.addListener(counter)
            TwitterStreamFilters.filterTwitterStreamByLocation(twitterStream, Array(Array(-122.75,36.8,-121.75,37.8)))
            TwitterStreamFilters.closeTwitterStream(twitterStream)
        }
    
    def filterTwitterStreamByLocation(twitterStream: TwitterStream, coordinates: Array[Array[Double]]) = {
        twitterStream.filter(new FilterQuery().locations(coordinates))
      }