Jump to content

Get XY of map


davefootball123

Recommended Posts

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,

Link to comment
Share on other sites

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 by davefootball123
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by davefootball123
Link to comment
Share on other sites

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 by davefootball123
Link to comment
Share on other sites

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 by davefootball123
Link to comment
Share on other sites

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;
   }
}

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.