CREATE TABLE `markers` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR (60) NOT NULL ,
`address` VARCHAR (80) NOT NULL ,
`lat` FLOAT (10, 6) NOT NULL ,
`lng` FLOAT (10, 6) NOT NULL ,
`type` VARCHAR (30) NOT NULL
) ENGINE = MYISAM;
markers
. To do this, you need to write a small code in PHP, during which the information placed in the URL will be written as a string in the database. The functions used should work in both PHP4 and PHP5. If you have never written to PHP code, during which the connection to the database occurs, then visit php.net and read about the functions mysql_connect
, mysql_select_db
, my_sql_query
, and mysql_error
.<?php
$username="username";
$password="password";
$database="username-databaseName";
?>
INSERT INTO
request. If in the process of processing or sending this data any errors occur, a message will appear on the screen that will help you find out what the problem is. To check the correctness of the script, add the following parameters to the URL ( name=
, address=
, lat=
, lng=
, type=
): http://www.yoursite.com/?name=Best%20Bar%20Ever&address=123%20Main % 20St & lat = -37.12345 & lng = 122.12345 & type = bar.<?php
require("phpsqlinfo_dbinfo.php");
// URL
$name = $_GET['name'];
$address = $_GET['address'];
$lat = $_GET['lat'];
$lng = $_GET['lng'];
$type = $_GET['type'];
// MySQL
$connection=mysql_connect ("localhost", $username, $password);
if (!$connection) {
die(' : ' . mysql_error());
}
// ,
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die (' : ' . mysql_error());
}
//
$query = sprintf("INSERT INTO markers " .
" (id, name, address, lat, lng, type ) " .
" VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
mysql_real_escape_string($name),
mysql_real_escape_string($address),
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($type));
$result = mysql_query($query);
if (!$result) {
die(' : ' . mysql_error());
}
?>
true
". Then, when you click on the marker itself, the info-window will open. This window will contain an HTML form with fields to fill in, a drop-down list and the Save button. Each element of our form will have an id
attribute, and when we click on the “Save” button, the saveData
function described below will work.GDownloadUrl
function;GDowloadUrl
function is 200. This means that all data was successfully transferred;<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.0 Strict // EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "content-type" content = "text / html; charset = utf-8" />
< title > Google Maps JavaScript API Example: Simple Map </ title >
< script src = "http://maps.google.com/maps?file=api&v=2.x&key=YOUR_KEY"
type = "text / javascript" > </ script >
<script type = "text / javascript" >
var marker;
function initialize () {
if (GBrowserIsCompatible ()) {
var map = new GMap2 ( document .getElementById ( "map_canvas" ));
map.setCenter ( new GLatLng (37.4419, -122.1419), 13);
GEvent.addListener (map, "click" , function (overlay, latlng) {
if (latlng) {
marker = new GMarker (latlng, {draggable: true });
GEvent.addListener (marker, "click" , function () {
var html = "<table>" +
"<tr> <td> Name: </ td> <td> <input type = 'text' id = 'name' /> </ td> </ tr>" +
"<tr> <td> Address: </ td> <td> <input type = 'text' id = 'address' /> </ td> </ tr>" +
"<tr> <td> Type: </ td> <td> <select id = 'type'>" +
"<option value = 'bar' SELECTED> bar </ option>" +
"<option value = 'restaurant'> restaurant </ option>" +
"</ select> </ td> </ tr>" +
"<tr> <td> </ td> <td> <input type = 'button' value = 'Save & Close' onclick = 'saveData ()' /> </ td> </ tr>" ;
marker.openInfoWindow (html);
});
map.addOverlay (marker);
}
});
}
}
function saveData () {
var name = escape ( document .getElementById ( "name" ) .value);
var address = escape ( document .getElementById ( "address" ) .value);
var type = document .getElementById ( "type" ) .value;
var latlng = marker.getLatLng ();
var lat = latlng.lat ();
var lng = latlng.lng ();
var url = "phpsqlinfo_addrow.php? name =" + name + "& address =" + address +
"& type =" + type + "& lat =" + lat + "& lng =" + lng;
GDownloadUrl (url, function (data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
marker.closeInfoWindow ();
document .getElementById ( "message" ) .innerHTML = "Location added." ;
}
});
}
</ script >
</ head >
< body onload = "initialize ()" onunload = "GUnload ()" >
< div id = "map_canvas" style = "width: 500px; height: 300px" > </ div >
< div id = "message" > </ div >
</ body >
</ html >
Source: https://habr.com/ru/post/38739/
All Articles