Hi,

I need to get the nearest neighbor state based on the IP address of the current state. I have done some research and found Geo plugin, which gives the state information based on the IP address. So now i need the States nearer to that IP address.How can i do it in PHP..? can any one suggest me..

Thank you.

Dani AI

Generated

Two different problems are commonly implied by "nearest neighbour state" and they need different solutions. As asked, and as pointed out, IP -> state is only the first step; deciding neighbours can mean either (A) the set of adjacent (bordering) states, or (B) the states closest to the user’s actual coordinates. IP lookups can be imprecise (ISP/POP locations), so choice of method should match required accuracy.

A — Fast / bordering-states approach
Keep a small static adjacency map (array or DB table) keyed by state code. This is the simplest and fastest for "which states border the detected state" and requires no geometry math.

Example (concept):

$neighbors = [
  'CA' => ['OR','NV','AZ'],
  'NY' => ['PA','NJ','CT','MA','VT'],
  // full mapping stored in JSON or DB
];
$state = 'CA'; // from GeoIP
$bordering = $neighbors[$state];

B — Coordinate-based (approximate): centroids + Haversine
If the goal is "nearest by actual distance" and GIS precision is not required, convert IP to lat/lon, keep one centroid per state, compute haversine distances and sort. This is simple PHP and fast, but centroid results are an approximation.

Haversine snippet:

function haversine($lat1,$lon1,$lat2,$lon2){
  $R=6371; $dLat=deg2rad($lat2-$lat1); $dLon=deg2rad($lon2-$lon1);
  $a=sin($dLat/2)**2 + cos(deg2rad($lat1))*cos(deg2rad($lat2))*sin($dLon/2)**2;
  return 2*$R*asin(sqrt($a));
}

C — Accurate / production: polygons + spatial queries
For true geographic accuracy (nearest border or exact adjacency), store state polygons (GeoJSON/Shapefile from official sources) in a spatial DB and use spatial functions. Example PostGIS queries:

-- containing state
SELECT name FROM states
 WHERE ST_Contains(geom, ST_SetSRID(ST_MakePoint(:lon,:lat),4326));

-- adjacent (touching) states
SELECT s2.name FROM states s1
 JOIN states s2 ON ST_Touches(s1.geom, s2.geom)
 WHERE s1.name = 'California';

-- nearest by true distance (meters)
SELECT name, ST_Distance(geography(geom), geography(ST_SetSRID(ST_MakePoint(:lon,:lat),4326))) AS meters
 FROM states ORDER BY meters LIMIT 5;

CREATE INDEX states_geom_gix ON states USING GIST(geom);

Notes and cautions
IP-derived lat/lon can be off by tens–hundreds of km for some ISPs; for fine-grained proximity prefer browser geolocation (user permission) or explicit user input. Decide whether corner-touching (four-corners) counts as a neighbour. For non‑US regions the same patterns apply but require the appropriate administrative polygons.

Mine is more an idea than a solution: you will probably need the position (in coordinates) inside the state, in order to evaluate the nearest border. For example, in MySQL, having the coordinates of each city, you can query and get the result for the cities in a defined range, the first of the result set outside the national border will be in the nearest state.

But the IP is not always the correct source city, a part proxies, sometimes you get the IP of a Tier 2 (or 3) node, for example if you check for italians IPs those can be matched to cities distant up to 800km from the real user location, depending on the node that will send the request out.

The Geolocation API can probably be more accurate, check for:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.