Google Maps API area search - php

I have a set of adresses / coordinates. In my software the user should be able to search for a specific adress or coordinate and find the surrounding places of the set of coordinates in a specific radius. The results should be shown on a map.
(Mostly like Google Places for Restaurants just with my own set of places)
Displaying the places on a static map is not the problem, but handling the set of addresses (~5000) and calculating the distances to the place searched for.
How can I establish a function like this using Google's APIs?
Thanks!

You can use the harvesine formula and a bounding box. Or you can use a spatial index and a bounding box. The harvesine formula is used for spheres. Most likely you use straight distance (like the crow flies).

Related

Search database for locations within polyline coordinates

I am using Google Maps to display locations stored in a MySQL database containing name, latitude and longitude information. An AJAX request is sent and the map is updated with all the locations. So far, so good.
Users have the ability to filter the locations by drawing polylines on the map to create a bounding box. The idea is that only locations within this bounding box are returned and they can draw whatever shape they want.
All the points used to draw the shape are returned a list of coordinates. Where I am struggling is taking the list of coordinates and finding everything within them. Can I achieve this with an SQL query? Is it even possible?
I have seen examples of returning locations within x radius from one set of coordinates and also a square box, but I have no idea how to take that further and get everything within a potentially infinite set of coordinates.
You should transform the shape the user enters into a polygon. Then you transform all locations within a square around this polygon into the same coordinate system.
Using a solution describe in the wikipedia page for point-in-polygon problems you will be able to find all locations within this shape and remap them to coordinates in your sql database and then into locations.
There are however similar question which might help you find a proper solution to your problem. See:
How can I determine whether a 2D Point is within a Polygon?
Point in Polygon Algorithm

API to get Center point zip code of a City

I am looking for an API that will give me all the zip codes within a 20 mile radius of a given US city. So far based on my online search, I could only find APIs that will give me all the zip codes with a 20 mile radius for a given zip code. Hence I’m trying to find an API that will (a) first find the zip code of the center point of a city, and then (b) use the API that I referred above to pass the zip code and the distance (e.g. 20 miles) to get all the zip codes within that 20 mile radius.
So I’m looking for (a) above, which is an API that will return the zip code of the center point of a city. This is all for US.
You can use the Google Geocoding API to get the location coordinates of a city, then use reverse geocoding to look up the actual zip code for that location.
Bear in mind if you get this data from Google you need to use it on a Google Map and are not supposed to use it outside of that.
Use the Geocoding API from Google to first map the city to the corresponding lat-long pair. Then use the Geonames API to get the postal codes in nearby radius. Use the following format for the Geonames API
http://api.geonames.org/findNearbyPostalCodes?lat=47&lng=9&username=demo
Find more details here
If you want a flat file instead, to maintain the data yourself, the USGS Gazetteer Populated Places file has really good city centroid coordinates and is free to use.

List points within a certain radius of a location?

I'm making an API for an iPhone app. Put simply, I have locations stored in a database (Lat/long). When a user send a request for points within, let's say, a five (5) mile radius, I'll need to make use of an algorithm to only give me those points from a MySQL database.
How can I convert miles to minutes in a coordinate? IE, with the tapering nature of longitude in mind, how much do I add to coordinates I'm searching through to compare to the users location?
2 What's a good formula to take into account the way longitude/latitude changes as you go to different points of Earth?
Thanks :)
You never mentioned how you stored the data, so I can't give you the actual code. Here's just something I came up with:
Import all the data into some efficient database. Optimize it to be efficient at pulling up coordinates within a certain X/Y range
Select all the coordinates within a 10x10 mile square. Import them into an array
Use a foreach to go through that array and use the distance formula (distance=sqrt((x-x)^2+(y-y)^2)). If within 5 miles, add it to another array. If not, ignore it.
I don't know how efficient this would be with thousands of coordinates in a small location; you might have to fine tune it (for example, try a smaller box first if you're finding the closest location).
I do have to agree with Darren that Google Maps API might be better suited, although I haven't used it before.

PHP Image Libraries

I'm working on a PHP application that needs to obtain data from a topographical map (different elevations denoted by different colors). I need to do two things:
Using this map (800x600), find a way to determine the exact pixel location of a particular city. For example, San Francisco is located at precisely 121x585.
Using the location from (1) above, I need to read the exact color at that location.
Note: The map provider does not provide location-based data, only a colored map. I suspect multiple libraries would be needed to map coordinates to locations on the map (via a ratio?) and then use OCR to read the color.
Are there any PHP libraries/tools that do this? How would you pull this off?
Once you know the pixel-coordinates, you can use PHP's built-in GD library to sample the color of an arbitrary pixel quite easily.
The tricky bit will be determining the pixel to sample, which can get pretty darned tricky. The earth being sphere-like, maps use various projections to produce a 2-d representation. If you know how the colored map image is projected, and you know the latitude/longitude of the pixel at (0,0), you should be able to write a function to convert lat/long to a pixel coordinate.
I may not have understood the problem entirely, but you should map the locations you want into an array (of objects?)
$city_mapping = array(new City("San Francisco", 121, 585), new City....); //Map your cities to an array.
Where City should be defined as a class to contain those variables.
Then use imagecolorat() to check for the color.

Calculate zipcodes in range

I have a website where users start by entering their zipcode.
I have a table with different shops which also contain the zip codes belonging to the shops.
Is it possible to get the zip codes within let's say a 5 km radius of the zip entered by the user?
So I can query the database and find all the shops near the user.
Can I use Google Maps for this? Should I get my hands on a pre-computed list of all zipcodes (single country) and their coordinates? Any other suggestions?
you can store longitude/latitude for each zipcode. That data is available for free on the net , one such is here http://www.boutell.com/zipcodes/. Then you would find the zipcodes in the radius by using great circle distance function which is discussed here MySQL Great Circle Distance (Haversine formula)
Google has a tutorial on this that should point you in the right direction. The bit of trigonometry in the SQL query is the only tough part. At first glance, there are basically two ways to go about this:
Get the lat/long coordinates for each store. You can do this using Google Maps' Geocoding API by providing the street address for the store. You'd only need to do this once for each location.
Only having the zip code for each store, and using a table of zip codes with geometrically-centered lat/long coordinates for each. Find the zip codes that are within range and then show the stores with those zip codes.
The first would be more accurate, but either works. If you're calculating distance from a zip code rather than an address, you'd still need to look up the lat/long coordinates for that zip code.
You'll need that latitude and longitude of each zipcode, and then use the Haversine Formula to get the approximate distance.
you need to get latitude/longtitude from the zipcode and then you can find places near by via maps or geoplaces api. head over to google maps api or YQL geo api.

Categories