UK postcodes for geocodes - google-maps

I've looked around for this most of the morning and haven't found a suitable way of finding UK postcodes from their geolocations (longitude / latitude) using Google Maps, is there anyone out there with any experience of this?
I understand Royal Mail has an API for the postcode search from a geolocation, but it's horribly expensive for such data, surely there has to be another way?
To note, the functionality I'm trying to achieve is that of: http://www.rightmove.co.uk/draw-a-search.html. When the shape is created, a list of UK postcodes (the first 3/4 digits of the postcode, for the area) must be given.
Thanks in advance.

You have at least 3 requirements
A list of UK postcodes with coordinates.
Drawing polygon on Map.
Locating postcodes within polygon.
Google for #1 (uk postcode coordinates free). Store data required in database
Use Google Shape library for #2
For # 3 you
Use the following query to eliminate the majority of postcode outside polygon.(Server side)
SELECT name, lat, lng FROM table WHERE (lat BETWEEN minLat
AND minLat )AND (lng BETWEEN minLng AND minLng)
to
Then use point in polygon to eliminate points outside polygon (client side)
function pointInPolygon(polySides,polyX,polyY,x,y) {
var j = polySides-1 ;
oddNodes = 0;
for (i=0; i<polySides; i++) {
if (polyY[i]<y && polyY[j]>=y || polyY[j]<y && polyY[i]>=y) {
if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
oddNodes=!oddNodes;
}
}
j=i; }
return oddNodes;
}
This Link shows #2 & #3

Related

The best way to get nearest street view by using getPanoramaByLocation with radium larger than 50 meters

I know that getPanoramaByLocation with radius less than 50 meters will return the nearest street view pano. However, for some locations, there is not always a street view. So what I am doing is like this:
function insider_function(locations) {
var place = new google.maps.LatLng(locations[local_idx].lat,locations[local_idx].lng);
sv.getPanoramaByLocation(place, 50, function(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
insider_function_2(locations[local_idx], data);
} else {
sv.getPanoramaByLocation(place, 100, function(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
insider_function_2(locations[local_idx], data);
} else {
alert('Internal error: ' + status + locations[local_idx]);
}
}
}
If radius 50 didn't return any result, I invoke radius 100. But still I am getting alert for ZERO_RESULT for some address. If I pass a very larger number, say 9999, I may get a very far street view for most other location with a closed street view.
So what is the best way to get the nearest street view pano with radius larger than 50.
I am not sure how google map do this.
For example, if I use "2300 Geng Rd, Palo Alto, CA" which coordinates are 37.4513333,-122.1214268. getPanoramaByLocation with radius 100 will return ZERO result but there is street view in :
https://www.google.com/maps/place/2300+Geng+Rd,+Palo+Alto,+CA+94303/#37.4513333,-122.1214268
A Google engineer in this thread explains this problem. Here are two options gained from that thread.
Use a radius of 1000 (1K) which works "most" of the time in getting the closest pano. (I have found this does work fairly well.)
Run two or more searches. Start the search with a large radius and work your way down, rather than starting small and working up. When a pano location is found with the large radius, use the distance between you search point and the resulting pano found as the radius value for your next search. According to the engineer's comments, you will "probably" only need to run two search requests.
If you do it with radius 200, it will display a street view, you can check out this jsfiddle link: http://jsfiddle.net/8ujnbh62/
Because there is no street view data around your address with radius 50. But if you provide a larger radius, it will show a street view.
From the Google Map link you provided (https://www.google.com/maps/place/2300+Geng+Rd,+Palo+Alto,+CA+94303/#37.4513333,-122.1214268) it does not give you the street view of "2300 Geng Rd, Palo, Alto, CA", instead it gives you the street view of the E Bayshore road, which is about 200 radius away from "2300 Geng Rd".
In your situation, you might just make another function with a larger radius, if the Google Map does not return you any street view data.

Random GPS coordinates in a non rectangular space

I am working on an user interface that shows many pins on a map.
During the development I am randomly generating 1500 map pins just to be placed on the map to test look/feel/performance etc. issues.
The code which does that looks like this:
for (var i = 0; i <= 1500; i += 1) {
$scope.mapPins.push({
latitude: (Math.random() * 2) + 51,
longitude: (Math.random() * 4) + 3,
icon: themeImages[Math.floor(Math.random() * themeImages.length)],
title: 'Sample title',
infoContent: 'Sample content'
});
}
Naturally the area of the pins covered is a rectangle for latitudes 51-53 and longitudes 3-7. For those who are wondering where it is, it is the area roughly around Netherlands.
Now, there's a little problem that the Netherlands is not a rectangular area and a lot of these coordinates fall over the sea and I would like my coordinates to be only on the land.
Is there a witty mathematical way how I can pool coordinates from a non-rectangular area?
Of course I could make a google.maps polygon object that covers a nonrectangular shape and then via google api test every random generated pin whether it falls within the bounds of this shape etc, but that would be an overkill for UI design phase. Basically my question is whether there is a neat mathematical trick that would allow me to randomly generate coordinates from a non-rectangular space.
Leave your code as it is, the rectangle is the bounding box over your area of interest.
Then add a line
if (isPointInpolygon(polygon, longitudeOrX, latitudeOrY) {
// use this location
}
now you only need to search for a point in polygon function, which is easy to find.
you can directly use the coordinates in (long, lat) order, longitude is related to x coordinate, lat to y.
The polygon has to be filled with the coordinates of the country not insode the water.
If you have islands, then maybe you need multiple such polygons, then iterate over all.
Not to be a stickler but you're actually generating 1501 map pins :)
It is very unlikely that you'll find a simpler solution than using a simple pointinpolygon check.
Use the Google Maps Drawing library (https://developers.google.com/maps/documentation/javascript/drawing#using_the_library) to draw a polygon around the boundary of the Netherlands and save it however you want (e.g., in database, or just copy the string that defines the boundary's coordinates).
Then in your script above, define the google maps polygon (similar to what is done here in the official docs: https://developers.google.com/maps/documentation/javascript/shapes#polygons), then use the containsLocation method in the Google Maps Geometry library (https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation) to check if your random map pins lie within the boundaries of the Netherlands before adding them to the map.
For example:
var netherlandsCoords = [
// comma-separated list of coordinates defining the Netherlands boundary
];
var netherlandsBoundary = new google.maps.Polygon({
path: netherlandsCoords
});
for (var i = 0; i <= 1500; i += 1) {
var lat = (Math.random() * 2) + 51;
var lng = (Math.random() * 4) + 3;
var latlng = new google.maps.LatLng(lat, lng);
if (google.maps.geometry.poly.containsLocation(latlng, netherlandsBoundary)) {
$scope.mapPins.push({
latitude: lat,
longitude: lng,
icon: themeImages[Math.floor(Math.random() * themeImages.length)],
title: 'Sample title',
infoContent: 'Sample content'
});
}
}

DbGeography.Distance() returning incorrect distance

I'm experimenting with System.Data.Spatial.DbGeography, that I want to use to determine the distance from one coordinate to another (going to be stored in SQL server).
My coordinates are in lat/long, and I got them from Bing Maps (I've tried with coordinates from Google Maps too, with the same result).
var osloCentralStation = DbGeography.FromText("POINT(59.9109 10.7523)", 4326);
var drammen = DbGeography.FromText("POINT(59.7378 10.2050)", 4326);
Console.WriteLine("Distance: {0}km", osloCentralStation.Distance(drammen) / 1000);
Returns:
Distance: 63,4340839088124km
The returned distance is approximately double what it should be.
https://maps.google.com/maps?saddr=59.9109+10.7523&daddr=59.7378+10.2050
Does anybody have any idea as to what's going on?
You're not declaring the element in WKT in the right order.
WKT should be in your case:
POINT(10.2050 59.7378)
See OGC standard here:
http://msdn.microsoft.com/en-us/library/bb933834.aspx
http://en.wikipedia.org/wiki/Well-known_text
And then it has to be declared like:
POINT(LONGITUDE LATITUDE)
Also keep in mind that it won't be the driving distance but the distance by air.
It turns out that lat/long are given as long/lat when creating new DbGeography objects.
I've written a little helper method so that I don't get it wrong again in the future:
private static DbGeography CreateDbGeography(double latitude, double longitude, int srid = 0)
{
var text = string.Format(CultureInfo.InvariantCulture.NumberFormat, "POINT({0} {1})", longitude, latitude);
if (srid > 0)
{
return DbGeography.FromText(text, srid);
}
return DbGeography.FromText(text);
}

Compute the distance between polyline (route) and marker in Google Maps API v3

Is there any convenient way to compute the direct (shortest) distance between Polyline (the route generated by Google Directions) and markers that are NOT situated on that polyline?
The only way I found out is to cycle through Polyline.getPath() vertices manually to calculate the shortest distance but it seems to be a bit harsh:
var path = routes[0].overview_path;
for (var i = 0; i < data.points.length; i++) {
var latLngA = new LatLng(data.points[i].lat, data.points[i].lng);
var shortest_distance = null;
for (var j = 0; j < path.length; j++) {
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA, path[i]);
if (shortest_distance == null || distance < shortest_distance) {
shortest_distance = distance;
}
}
console.log(data.points[i].point_title, shortest_distance);
}
Thanks in advance!
As far as I know, the Google Maps API does not give you a way to do this easily. And unfortunately, the algorithm you use will not give an accurate answer, because it gives the distance from the marker to the closest vertex on the path, not the closest point on the polyline itself, which will usually not be one of the points.
If you really need an accurate calculation, the best option I know of is to use the Javascript Topology Suite (JSTS). JSTS has a ton of geographic formulas for calculating this sort of thing. That means converting the polyline returned from the directions API into a JSTS object and calling the right utility function. Not trivial, but not too difficult either.
Use turf - modular geospatial analysis engine.
https://turfjs.org/docs/

google maps : How to find if any markers inside map

I have divided the my google map display in to numbers of parts, Now I want of find it out if any markers are positioned inside a/any particulate cell.
Any Help ?
Farther Explained :
I have got the map bounds by
map.getBounds();
method and then farther divide it into numbers of sub-bounds.
also I have putted markers as
map.addOverlay(markerObject);
Now , I want find if of the cells (which I got by dividing the map by bounds) is containing any markers or not .
I have divide the entire map bounds into numbers of sub bounds
So keep all markers in array. Each marker has a method called get_position( ). After you have finished division of map bound into small sub bounds, you just need to iterate over the sub bounds and check whenever the marker within it.
PS. Also take a look on it, in some cases could be useful.
Suppose you on sub bound cell:
var sub_bounds = new Array();
// here you've pushed into an array the sub bounds
for ( var i = 0; i<sub_bounds.length; ++i)
{
for ( var j = 0; j < markers.length; ++j)
{
var lat = markers[j].get_position( ).lat;
var lng = markers[j].get_position( ).lng;
if ( sub_bounds[i].sw.lat<lat && lat<sub_bounds[i].ne.lat &&
sub_bounds[i].sw.lng<lng && lng<sub_bounds[i].ne.lng)
// marker within cell, do whatever you need to do
}
}
Here is an alternative to the above solution without re-iteration:
First - how big are your sub_bounds? Say 10 latitude and longitude degrees each.
Second - The position of the marker is (floor(marker.lat / 10), floor(marker.lng / 10))
Third - Each marker is added to the map and dropped in a bucket for that subdomain.
so (40, -78) would lie in bucket (4,7) i.e. bucket["4~7"]
Correction: would lie in bucket (4,-7) i.e. bucket["4~-7"]
which would contain all markers between 40 and 50 lat and -70 and -80 lng.
You can use GLatLngBounds as the object that holds all these markers in each bucket, which would give you a good set of methods to use, such as calculating center of the bucket depending on the markers currently in it.
Probably the best solution is given here: how to find out whether a point is inside a polygone:
How to detect that a point is inside a Polygon using Google Maps?