Geocoding Tweets on a Google Map - google-maps-api-3

Would really appreciate any help with my issue. I've been looking at this terrific tutorial by Marcin Dziewulski who integrated recent tweets and twitter user avatars onto a Google Map. Basically he places the users avatar where they last tweeted. I highly recommend checking the tutorial out here:
http://tympanus.net/codrops/2011/04/13/interactive-google-map/
It all seems to make sense except when it comes down to the actual geocoding of the user. The code works beautifully when the different tweeters are from different cities. However, let's say you wanted to track certain users in one city. They're all geocoded from the center of the city and you can only see one avatar at a time. If you refresh the page, another one will show up, in the exact same spot. This obviously isn't what I want. I assume the problems is in this bit of code:
var users = o.twitter.get(), arr = new Array;
for (i in users){
var user = users[i];
$.getJSON('http://twitter.com/users/show/'+user+'.json?callback=?', function(data) {
var img = data.profile_image_url,
screen_name = data.screen_name;
geocoder.geocode({ address: data.location }, function(response, status){
if (status == google.maps.GeocoderStatus.OK) {
var x = response[0].geometry.location.lat(),
y = response[0].geometry.location.lng();
marker = new google.maps.Marker({
icon: img,
map: map,
title: screen_name,
position: new google.maps.LatLng(x, y)
});
I think something needs to change in the geocoder.geocode({ address: data.location } bit of code, but not 100% positive. I can't seem to find the solution in the Twitter API documentation either.
My question is basically this...can the code above be altered so that it gathers more precise lat and long. coordinates from where tweets are actually generated and then display them correctly at those locations? In other words, don't just put a tweet from NYC in the middle of NYC, but place the avatar at the EXACT location.
I've tested this with my own tweets, with the the location feature of twitter enabled. I just can't figure this out! Thanks again to anyone who can help!
Best,
Brandon

The problem is that the vast majority of tweets are not geocoded but the coords are simply guessed from the user's location settings. If you wanted to display this in an interesting way, consider doing a uniform distribution of "center of city" latlongs. This is what census data map-makers often do when they don't have any more exact positioning data than say a district or a tract.

Unless the tweet was made from a mobile device it's lat/long is going to be where the ISP is registered. If I tweet from my desktop my tweets emanate from the street in front of the capitol here in Nashville. If I use my phone and I allow the twitter app to use my lat/long then that would be different. Trust me, I could instantly use the lat/long data in analysis of twitter streams and I wish it were that simple.
What you could do is add a jitter function that adds random displacement along both the x and y coordinate field to displace the tweets lat/long. Or you could grid it out. If you know how many of them you are getting you can figure out easily the bounding area of your grid and do it like that.

Related

Quality of result set

I use the here PlacesServices to retrieve information what's around me. Often I get results that are quite ambiguous / duplicate because outdated data appears to be in the result set that reduces the quality quite significantly. How can we feed back changes to the community or how and when does here get updates for those categories, e.g. restaurants or petrol-stations?
Is there a way to dedup?
This is a good example for 3/6 duplicates (same petrol station) since the chain changed some time ago dependent of the direction on the highway.
https://places.ls.hereapi.com/places/v1/places/276u1jne-8c62ebd6159c441eba290df4efdcfd1d;context=Zmxvdy1pZD02ZWQ0YzViNS0wMzgxLTUxZDAtOTg2ZC00NjQ3YTVjNWJhYTJfMTU3NTY2NjU1MTYwM18wXzE1NCZyYW5rPTI
https://places.ls.hereapi.com/places/v1/places/276u1jne-3c240a265c6e48d698a400b7d8738202;context=Zmxvdy1pZD02ZWQ0YzViNS0wMzgxLTUxZDAtOTg2ZC00NjQ3YTVjNWJhYTJfMTU3NTY2NjU1MTYwM18wXzE1NCZyYW5rPTQ
3 chains for a single petrol station
Finally, is there a solution how I'd only obtain those in my travel direction?
var query = {"in": lat +"," + lng +";r="+distance*1000,"cat" : categories +",pretty"};
let entryPoint = H.service.PlacesService.EntryPoint;
await this.places.request(entryPoint.EXPLORE, query,
function(response) {
values = response.results.items;
}, function(resp) {
console.log('ERROR: '+resp);
});
Best regards and many thanks in advance
O.
In general the feedback related to HERE Map data can be reported by either the Map Feedback API , the details on the API are available on the documentation page , or The Online Tool HERE Map Creator could be used.
With respect to retrieving POIs, it is possible to request POIs along the route using the "browse/by-corridor" (documentation) end point where the route shape and a radius could be provided as a corridor. It should be noted however the API does not consider the heading direction of the route and may return POIs on the other side of a road. Example :
https://places.ls.hereapi.com/places/v1/browse/by-corridor?route=[52.5199356,13.3866272|52.5100899,13.2816896|52.4351807,13.1935196|52.4107285,13.1964502|52.38871,13.1557798|52.3727798,13.1491003|52.3737488,13.1154604|52.3875198,13.0872202|52.4029388,13.0706196|52.4105797,13.0755529]&apiKey=API_KEY

Is it possible to send a zip code to maps V3 API to return a map centered on that zip code?

I need to display a map on a web page - I have the user's address info, but for security purposes I need the map to be centered on the centroid point of their zip code or city, not their specific address.
Is it possible to submit only a zip code to the Google maps V3 API and have it return a map of a postal code / zip-code?
Or do I have to first geocode the zip code and submit the lat/long?
This is how I was doing it using Yahoo...
//assign the zipcode from the database to the zipcode variable
zipCode = oUser("zipCode")
<script type="text/javascript"
src="http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=x">
</script>
// create the container
<div id="map" class="map">
<script type="text/javascript">
// Create a map object
var map = new YMap(document.getElementById('map'));
// Display the map centered on the zipcode
map.drawZoomAndCenter("<%=zipCode%>", 5);
</script>
If you go to google.com/maps and enter a 5 digit US zip code, the website will return a map that is centered on the central point of that zip code. The script above did the same thing on my website (until Yahoo discontinued its map service).
I’d like to do the same thing using the Google API – but everything I’ve seen so far indicates that I have to send the long/lat string of the zip-code centroid point to the API - which is fine (we have a zip-code/long/lat table in the database) – it just requires more server work on our end – or on Google’s if I geocode a zipcode on the fly). I’d like to keep this as efficient as possible and send only the postal_code if possible.
Can that be done?
Thanks,
Michael

Dynamically set bounds on nearbySearch based on search critieria

We've implemented a search box and google maps on our page to allow customers to perform searches based on places queries, and so far it's working well. However, using TextSearch we almost always get 20 results (unless it's a specific point). What we prefer though, is to return a set of results that makes more sense to a user based on their search (i.e. if they're searching for churches within a zipcode, we shouldn't show churches outside the zip code).
I know we can bias our results based on location and radius, and even restrict results based on location / radius using NearbySearch.
However, our customers are national users who may be searching in any area in the world, so we're not sure, until the user searches, what location and radius to set as a restriction. I'd like to determine that dynamically based on their query.
For example, in Google Maps if you search for "Churches near 30319" you get a much more localized result set than "Churches near Georgia"
Churches near 30319:
https://maps.google.com/maps?q=churches+near+30319&hl=en&sll=33.772251,-84.296934&sspn=0.049158,0.082312&hq=churches&hnear=Atlanta,+Georgia+30319&t=m&z=14
Churches near Georgia:
https://maps.google.com/maps?q=churches+near+Georgia&hl=en&sll=33.870438,-84.332304&sspn=0.049102,0.082312&hq=churches&hnear=Georgia&t=m&z=8
I've tested doing a separate query using geocode to get the single-point location of the query. i.e.
getGeneralVicinity = ->
address = $('#address').val()
window.oneq.geocode.geocoder.geocode
address: address,
(results, status) ->
if status is google.maps.GeocoderStatus.OK
console.log(results[0].types)
It seems by possibly finding the type of the geocode result (i.e locality) we could determine a radius and use the geometry.location for the location bounds. Unfortunately, it's not consistent, and if a user only searches for "churches", this doesn't give us the desired results.
Any help would be greatly appreciated.
You almost had it. I will refer to the Geocoding API as its JSON feed rather than the Google implementation of it, so detail will come straight from the source. There are some very interesting parameters that come back when geocoding something. Try it:
Georgia: http://maps.googleapis.com/maps/api/geocode/json?address=georgia&sensor=true
30319:http://maps.googleapis.com/maps/api/geocode/json?address=30319&sensor=true
Both of the second-tier address components are Georgia. However, there are a few differing parameters, and the one you want is geometry. This indicates the shape of the area.
Take, for example, the 30319 request. You will get as bounds:
"bounds" : {
"northeast" : {
"lat" : 33.9203610,
"lng" : -84.30943599999999
},
"southwest" : {
"lat" : 33.83286890,
"lng" : -84.35826589999999
}
},
"location" : {
"lat" : 33.87309460,
"lng" : -84.33842899999999
},
This tells you three things:
The corners of the bounding box
The centre of the box (which will be the intersection of the vertex lines)
This allows you to compute the maximum distance from your centre, which you can then feed back into your google places API search as radius. Conversion from lat/long to distance is trivial: it's called the orthodromic path. Two formulas exist - one for small distances (Haversine's formula), the other for large distances (this). Someone wrote a calculator for these: http://williams.best.vwh.net/gccalc.htm . You'll quickly see that the bounding box for 30319 spans 10km, whilst the georgia one spans almost 700 (which would require multiple Google Places requests to match).
Let me know if this wasn't clear and I'll elaborate further.

Google Maps API: Bring up address selections as you type

I'm looking to create a web application that starts to suggest home addresses as you type. For instance, imagine a pizza delivery company, where you start typing in your address, "1279", and beneath the box it brings up 1279's in the US for people to choose from, like:
1279 Main Street, St. Louis, MO
1279 Tree Street, Baltimore, MD
In this way, it would really mirror maps.google.com in bringing up suggestions as you type.
I've looked through the Google Places and Maps APIs without much success. The GeoCoding one works OK by passing an address parameter through, but often returns no results or really bad ones... nothing like maps.google.com. Plus they're difficult to parse. (The address parts parameters aren't always consistent, meaning that I have to send the formatted address through another parser... not a deal-breaker though.)
Anyone else have any suggestions out there? Thanks! Jeremy
You can improve the Places autocomplete results by passing bounds option when creating it. The example binds it to the map viewport:
autocomplete.bindTo('bounds', map);
In this demo I hardcoded the continental US bounds (plus some of Mexico and Canada)
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input,
{bounds: new google.maps.LatLngBounds(
new google.maps.LatLng(23.730197707069532, -126.14240169525146),
new google.maps.LatLng(50.1805258484942, -65.32208919525146)) }
);

Can I find out if a coordinate is inside a city?

Let's say I have a LatLng object, is there any way of checking if it represents a possible location within a city? How can I get the limits of a city?
I'm using Google Map V3.
Have you tried reverse geocoding?
http://code.google.com/apis/maps/documentation/javascript/services.html#ReverseGeocoding
You could check the formatted address to see if the city matches what you're looking for. I'm not sure how accurate this will be for your application, but it's an option.
function codeLatLng() {
var geocoder = new google.maps.Geocoder(),
latlng = new google.maps.LatLng(40.730885, -73.997383);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
console.log(results[1]);
console.log(results[1].formatted_address);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
City limits and other municipalities are constantly re-drawn. There are certain services that exist to help you find them, but I'm not positive that Google keeps a record of city limits inside their data for the Google maps. Here's a discussion in google groups about it. A snippet from that site:
Depending where you are in the world, city limits and other
administrative boundaries are CONSTANTLY being changed, and it's even
sometimes difficult for local governments to keep their data current
because of annexations and other changes. Also, 'city' might be
something relative small or the size of Shanghai. Also, different
countries can also have sometimes conflicting definitions what
administrative unit is the actual definition - a good example is
China. Probably your best approach is to get your data from a local
government or a data supplier and build your own.
You can use Reverse Geocoding in the Google Geocoder API and check the locality entries and perhaps sublocality entries of results returned. http://code.google.com/apis/maps/documentation/geocoding/#Types
Note that this probably won't work so well if you have addresses all over the world and want to know "What city is this LatLng in?" On the other hand, it will probably work reasonably well if you want to know "Is this LatLng within Chicago?" There are areas of the world where the data is fairly complete and sensible, and areas where it is incomplete and/or organized in ways you might not expect. (Apparently, the UK uses "county" and "state" very differently from the USA, for example. Even if I'm wrong about that, you get the idea.)

Resources