Caricare markers da un database con MySQL e PHP (prima parte) Una possibilità davvero interessante è quella di poter visualizzare su una Google Map dei
markers le cui coordinate ed altri elementi siano caricati da un database esterno.
Questo ci consente di poter utilizzare insiemi molto grandi di coordinate lasciando sempre inalterato il codice javascript di richiamo della map.
This article does not conclude our work, we will figure out how to build our database, populate it and create PHP scripts to query the data and the creation of an XML output. We therefore find ourselves in the same situation the previous post: an XML file to be given "at the mercy" to the javascript code so that the markers are displayed on the map.
This post and the next to make extensive reference material, suitably simplified and integrated into the popular, which appears in very good article (in English) you find on Google Code
.
define the project to realize a sort of "geographical curriculum," a Google Map where they are see all the places where I studied or worked, whose details are contained in a database.
As a teaching project, the data are actually very few. The possibilities are endless but, as we could insert into the database large amounts of data created by third parties and, with appropriate technical knowledge, filter them in a manner that concerns us.
What you need:
1) A web space with support
MySQL and PHP
2) Some hints on how to build a relational database
3) Small knowledge
PHP and Javascript
POINT 1
There are two possibilities: the first is to register on the Internet to a service of free web space with the characteristics mentioned above. I have already mentioned
ilbello.com hosting, but the services of this kind are innumerable. A selection of the other free services and free you can find it in this post
.
The second option is to work locally, that is, use your PC like a web server. To do this you can use software packages such as XAMPP
or
EasyPHP. For proper installation, you can refer to the link at the bottom of the article.
POINT 2
we rely on a relational database, structured in tables (we will only need one). Our web space, be it real or virtual, will allow us to create and populate (insert data) a database through the language of administration
PhpMyAdmin.
As for the service will open the ilbello.com PhpMyAdmin at
http://sql.ilbello.com/ , inputting
username and password
provided to us upon registration. For other services, of course, will change the address but the access mode will be identical.
to create and populate the database table is posono use different methods:
1) inserting each element through the PhpMyAdmin interface
2) Amount in PhpMyAdmin appropriate sql files like this:
CREATE TABLE `markers` (`id`
INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `denom
`VARCHAR (60) NOT NULL,` address `VARCHAR
(60) NOT NULL,` lat `FLOAT
(10, 6) NOT NULL,` long
`FLOAT (10, 6) NOT NULL,`
tipomarker `VARCHAR (30) NOT NULL,
) ENGINE = MYISAM;
What do I say through this set of instructions?
create a table with 6 fields: the id field is an index that is autoincremente, the second campo, denom, ha al massimo 60 caratteri; lo stesso dicasi per il campo indirizzo; lat e long sono campi numerici, al massimo di 10 cifre di cui 6 decimali; tipomarker ha al massimo 30 caratteri; MYISAM è il motore di indicizzazione del database.
3) Importando opportuni files in formato
CSV (ottenuti da terze parti o dal nostro
foglio elettronico ).
PUNTO 3 Attingiamo adesso ad alcuni elementi di PHP per accedere ai dati del database ed elaborarli in modo da formare un documento XML. Per motivi di sicurezza si sceglie di creare due script distinti, uno con le credenziali di autenticazione and the other receiving the credentials, accesses the database and form the XML document. Here are the listings of the two files, and credenziali.php
creaxml.php . Please note that clicking on that link will not show the script but its result, ie the XML file with data displayed on the map.
\u0026lt;?
/ / Of course, $ username, $ password $ database will contain the values \u200b\u200breported by
your hosting service provider or chosen at will if you use a local web server
$ host = "localhost";
$ username = "vostrousername "
$ password =" YOURPASSWORD "
$ database =" vostrodatabase "
?>
\u0026lt;?
php / / require login information contained in credenziali.php
require ("credenziali.php");
/ / Replaces some special characters with the code entities' 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;}
/ / Connect to the database
$ connection = mysql_connect (localhost, $ username, $ password);
if ($ connection) {
die ('Not connected:'. Mysql_error ());}
$ db_selected = mysql_select_db ($ database, $ connection);
if (! $ db_selected)
{die ('no database selected:'. mysql_error ());}
/ / Select all rows in the table with markers
$ query = "SELECT * FROM markers WHERE 1";
$ result = mysql_query ($ query);
if ($ result) {
die ('Query invalid:'. mysql_error ());}
header (" Content-type: text / xml ");
/ / Generate the root node of the XML file
echo '\u0026lt;markers>';
/ / Loop that creates the rest of the XML document based on the data of Table
while ($ row = @ mysql_fetch_assoc ($ result)) {
/ / ADD TO XML DOCUMENT NODE
echo '\u0026lt;marker';
echo 'denom = "'. parseToXML ($ row ['denom']). '"';
echo 'address ='". parseToXML ($ row ['address']). '"';
echo 'lat ="'. $ Row ['lat']. '"';
echo 'long ='". $ Row ['long']. '"';
echo 'tipomarker ="'. $ Row ['tipomarker']. '" ';
echo'/>';
} / / Generate the closing tag of the root node
echo' \u0026lt;/ markers> ';
?>
For now we have finished in the next article we will see how to integrate everything into the Google Map, using instructions which we now know the meaning.
Linkografia for those who want to install a server on the local
@ To install XAMPP Joomla Course in Second Life (see the slides from 6 to 22) Slideshare @ (See slides from 6 to 22) @ XAMPP step by step @ Install XAMPP Windows PHP Easy to install PHP on Windows Help
@ @ Install and configure PHP and MySQL