📜 ⬆️ ⬇️

What should we build a user card?

Few weeks? I read back on Habré about integration with google maps. Unfortunately I did not find the link, but the point in the article was about the city site to which users could independently add objects to the map. The idea looked quite sexy, but in the comments the author admitted that he had been writing this functionality for several months ((

This fact plunged me into a hard gloom (
Script analysis on pretty gmaps integrated sites like this
dom showed impressive miles of code ... And so I would continue to sit in depression, until I accidentally wandered onto a site with a help from Google itself .

And as it turned out - all the information about the timing and scope of this task is at least sabotage and provocation! So, we build a user map based on Google maps service .
Something like this:

')

Uff, well, try. The right Programmers always start learning the documentation by examining the examples. Unfortunately, I usually finish at this stage) Anyway, the examples are written in full
And they are pretty good. Oh, these are some other examples, well, let them be, but in general I learned from these .

So, for the preparation of a primitive user card, we need:
- One table
- 1-2 html
- a couple of PHP
- a couple of scripts
- Maps API Key
One head and at least one finger. Total 3-4 screen code, we succeed.

Since Habr is not friendly to syntax highlighting of source codes - I will give links to them and briefly describe what is there.

The table we need:
CREATE TABLE `prefix_markers` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(180) default NULL,
`hello` varchar(180) default NULL,
`lat` float(10,6) NOT NULL default '0.000000',
`lng` float(10,6) NOT NULL default '0.000000',
`sex` varchar(4) NOT NULL default '',
`login` varchar(32) NOT NULL default '',
PRIMARY KEY (`id`)
)


Now it is necessary to fill in the record. This requires html where we show the map, we write in html, in the area of ​​the hider, something like this:

<? $loadmaps = "" ?>
<? if (strstr($_SERVER["REQUEST_URI"],'maps')) {?>
<script src="http://maps.google.com/maps?file=api&v=2.x&key= API"
type="text/javascript">

<? if (strstr ($ _ SERVER ["REQUEST_URI"], 'add')) {?>

<? $ loadmaps = "onload = 'initialize ()' onunload = 'GUnload ()'"?>
<?}?>
<? if (strstr ($ _ SERVER ["REQUEST_URI"], 'show')) {?>

<? $ loadmaps = "onload = 'load ()' onunload = 'GUnload ()'"?>
<?}?>
<?}?>



Sorry, but if everything is ugly formatted there - I'm not guilty)
There, depending on the url variable loadmaps is filled
which is then output here: <body <?=$loadmaps?>
Such a perversion - because I have one site header, but depending on what is needed - “show” or “add” to the map - different scripts are connected. I promise it will be easier further. Maybe)

Accordingly, inside the html body that is responsible for the display on the map, we write:


And which is for adding:
/>
/>





Uff, with htmlkami sorted out. Now we have 2 htmlki with empty cards. It remains to make the first people add, and the second - the display)

Add script - here it is, the villain

It seems everything is clear. In initialize - the marker is created, when clicked, the fields are shown where we enter the solemn "Preved"
ShowAddress shows a map centered on the address entered by the user.
In saveData, data is saved by calling a URL with parameters via AYAX
new Ajax(urlinsert, {method: "post"}).request();

This library's mootools syntax, it must also be connected to the hider as)

And, even there, the escape function is used everywhere, this is an incorrect function, because my website is also incorrect. In the wrong code page 1251. You do not need it. Maybe. In any case, judging by the comments on the habr of the wrong sites on the Internet do not exist, everything is only in the correct UTF-8. Well, except for the actual Habra and a couple more sites.

Taaks, then this script causes us to have a file insert.php. Everything is boring there, generally tin, just the records are inserted:
// Selects rows in the markers table 4 current user
$query = "SELECT id FROM prefix_markers WHERE login='".$login."'";
$result = mysql_query($query);
$row = @mysql_fetch_assoc($result);
//$hello = mb_convert_encoding($hello,"Windows-1251","UTF-8");

if ($row['id']>0){
//update row with user data
$query = "UPDATE prefix_markers SET " .
" name='".urldecode($name)."', hello='".$hello."', lat='".$lat."', lng='".$lng."', sex='".$sex."' ".
"WHERE id = '".$row['id']."'";
$result = mysql_query($query);
$res = $row["id"];
}else{
// Insert new row with user data
$query = "INSERT INTO prefix_markers " .
" (id, name, hello, lat, lng, sex, login ) " .
" VALUES (NULL, '".$name."', '".$hello."', '".$lat."', '".$lng."', '".$sex."', '".$login."')";
$result = mysql_query($query);
$res = "Insert";
}


Boredom ...
But after the request is fulfilled, we immediately open show.php
newurl = currurl.substring(0,currurl.length-4 ) + "/show";
..
new Ajax(urlinsert, {method: "post"}).request();
self.location.href=newurl;


Here it is. But it is empty) It stupidly opens the html show
$template = "themes/".$config['theme']."/templates/".$config['templates']['user_maps_show'];
viewTemplate($template);


Doot. Now the show script comes onto the scene.
There everything is clear. A blue (not blue!) Icon is created for boys, red for girls, markers are created, boring. The only suspicious moment is the urlxml = baseurl + "/ user / maps_xml.php" call;

Here he is:
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}

$query="SELECT *, (SELECT id FROM prefix_user as usr WHERE usr.login = markers.login) AS usrid FROM prefix_markers as markers";
$result = mysql_query($query);
//$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

//header("Content-type: text/xml");

// Start XML file, echo parent node
echo '';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'hello="' . $row['hello'] . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'sex="' . $row['sex'] . '" ';
echo 'login="' . $row['login'] . '" ';
echo 'usrid="' . $row['usrid'] . '" ';
echo '/>';
}

// End XML file
echo '';

exit();


Hurray !!! (Tortured to write) Oh yes. According to the mind - it should be a static file, not a script cat. in xml from database unloads on the fly ...

Vooot. In general, all this is just pieces of code with examples of Google combined in one piece. Surely you can mix them in a different sequence with other pieces and you will get a completely different result) Much better - 100%

See what should happen in the end after all these dances with a tambourine - you can here (carefully, the site contains erotic materials)
Button "map". Without registration - it does not work because ... - but they themselves have just seen all the sources) Therefore it does not work)

PS: Some terrible article turned out. And in style and availability. One hope that can help someone) If everything is bad and incomprehensible - write plz in the comments I will try to refine.

Source: https://habr.com/ru/post/17761/


All Articles