davefootball123 Posted February 10, 2013 Share Posted February 10, 2013 Hi all. I have a map located here http://www.sowx.ca/warnings.html and you can't see on there but I have a bunch of locations on the map defined by single x and y coordinates that represent cities. For example Windosr on that map is at x=117 and y=385. Is it possible to match the xy of a map click to the nearest defined xy point? Any help would be greatly appreciated. Thanks Dave, Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 Display in a form as input type image, name map When you click the image the form will be submitted and the click position will be in $_POST['map_x'] and $_POST['map_y'] Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) Thanks I managed to get the x and y coordinates working good. I just don't know how to compare the clicked coordinates to the array of defined coordinates and get the closest one. As you can see here. When you click the map. The x and y are displayed at the top and the default warning location is displayed. I have a set of x and y points for each location and need to compare the clicked xy to the array of points Edited February 10, 2013 by davefootball123 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 Pythagoras INSERT INTO city(xpos,ypos,cityname) VALUES (40,60,'manchester'), (80,60,'crewe'), (90,160,'stoke'), (120,30,'bradford'), (140,160,'norwich'), (100,200,'london'); SELECT cityname, SQRT((xpos-100)*(xpos-100) + (ypos-60)*(ypos-60)) as dist FROM city ORDER BY dist LIMIT 1 Results: crewe 20 Assuming 100,60 was clicked Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 Thanks, as you can tell I'm kind of new to php. What exactly does that do? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 it shows you how to query a database table of cities to find the nearest to the point clicked Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) Thanks again. How would I add this to my script right now. Here is the part that has to deal with the map x and y. What I basically need there is to have a bunch of x and y with the city name and set the location with the nearest city name. $map_x=$_GET['map_x']; $map_y=$_GET['map_y']; $map_location = $_GET['location']; $map_province = (isset($_GET['prov']) ? $_GET['map_prov'] : 'ON'); $map_wfo = (isset($_GET['wfo']) ? $_GET['map_wfo'] : 'CWTO'); Edited February 10, 2013 by davefootball123 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 ... I have a set of x and y points for each location and need to compare the clicked xy to the array of points Where? Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) Where? Sorry didn't show them there..I have to still get them all but its basically it is x,y and then city name and what I need to do is set the city name to the nearest one via the x,y click... if map.x=150&map.y=503 city=windsor if map.x=494&map.y=375 city=hamilton Sorry for being a pain. I appreciate the help though. Edited February 10, 2013 by davefootball123 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 (edited) Pardon me for for not being psychic but as you don't recognise SQL code I have to assume they are not in a database table. So I ask the question again "Where are you storing the cities and coordinates?" A sample would be nice. Edited February 10, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) Pardon me for for not being psychic but as you don't recognise SQL code I have to assume they are not in a database table. So I ask the question again "Where are you storing the cities and coordinates?" A sample would be nice. Sorry I'm not very good with array's. I'm not sure if you can see my idea here or not. if (($map_x ='494') & ($map_y='375')); { $map_location=('Hamilton'); } Thanks again. Edited February 10, 2013 by davefootball123 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 OMG have you really got dozens of statements like that to store x,y, city ? 1. It should be && and not & 2. There should not be a ; after the if expression 3. The data stored like that is not processable. Bite the bullet an use an array (if you must hard-code them and not store in a DB) eg $locations = array ( 'Hamilton' => array (494, 375), 'Buffalo' => array (530, 600), 'Kitchener' => array (450, 300) ); You can now loop through that array and calculate distances to find the nearest. If user clicks on 500,600 $x = 500; $y = 600; $min = 999999; $closest = ''; foreach ($locations as $city => $coords) { $dist = sqrt( ($coords[0]-$x)*($coords[0] -$x) + ($coords[1]-$y)*($coords[1]-$y)); if ($dist < $min) { $min = $dist; $closest = $city; } } } Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 Thanks, can I have $x and $y as the map click x and y? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 10, 2013 Share Posted February 10, 2013 Clearly not. Sorry. Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted February 10, 2013 Author Share Posted February 10, 2013 Clearly not. Sorry. Indeed I did get it to work that way. Map_x and Map_y clicked locations are now the $x and $y Barand Thank you so much for your help. It is working wonderful now. Thank you so much. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 11, 2013 Share Posted February 11, 2013 Marking as solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.