id, city, post_code, street, house_number, flat_number. Tak jak pisałem wcześniej, lokalizację pobierane są po nazwach (Nie współrzędnych).
Gotowy kod ma wyglądać następująco ( to jest skopiowany kod z https://developers.google.com/maps/document...rker-clustering tylko różnica jest taka, że u mnie lokalizację muszą pobierać się z bazy)
Kod
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Map</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 50%;
width: 30%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: 50.0614300, lng: 19.9365800}
});
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
});
});
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
var locations =
[
{lat: 49.9873800, lng: 20.0647300},
{lat: 49.9752400, lng: 19.8286900},
{lat: 49.8338300, lng: 19.9383000},
{lat: 50.0614300, lng: 19.9365800}
]
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap">
</script>
</body>
</html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Map</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 50%;
width: 30%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: 50.0614300, lng: 19.9365800}
});
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
});
});
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
var locations =
[
{lat: 49.9873800, lng: 20.0647300},
{lat: 49.9752400, lng: 19.8286900},
{lat: 49.8338300, lng: 19.9383000},
{lat: 50.0614300, lng: 19.9365800}
]
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap">
</script>
</body>
</html>
Mój kod, aktualnie wyświetla tylko 1 adres z bazy
Kod
****PHP****
<?php
$link = mysqli_connect("localhost", "root", "", "google_map");
$id = 2;
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
$query = "SELECT `city`, `post_code`, `street`, `house_number`, `flat_number` FROM `map` WHERE `id`= $id";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_row($result);
echo printf("%s, %s, %s, %s, %s", $row[0], $row[1], $row[2], $row[3], $row[4]);
if($row[4]== "") {
$address = $row[0].', '.$row[1].', '.$row[2].', '.$row[3];
}
else {
$address = $row[0].', '.$row[1].', '.$row[2].', '.$row[3].', '.$row[4];
}
include("maps.php");
?>
****HTML****
<!DOCTYPE html>
<html>
<head>
<title>Google Maps</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style>
#map {
height: 50%;
width: 30%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input id="address" type="hidden" value="<?php echo $address; ?>">
<input id="submit" type="hidden" value="Geocode">
<div id="map"></div>
<script src="geo.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap">
</script>
</body>
</html>
****JS****
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: 51.297, lng: 22.886}
});
var geocoder = new google.maps.Geocoder();
geocodeAddress(geocoder, map);
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
var infowindow = new google.maps.InfoWindow({
content: document.getElementById('address').value,
maxWidth: 300
});
marker.addListener("click", function() {
infowindow.open(map, marker);
});
}
});
}
<?php
$link = mysqli_connect("localhost", "root", "", "google_map");
$id = 2;
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
$query = "SELECT `city`, `post_code`, `street`, `house_number`, `flat_number` FROM `map` WHERE `id`= $id";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_row($result);
echo printf("%s, %s, %s, %s, %s", $row[0], $row[1], $row[2], $row[3], $row[4]);
if($row[4]== "") {
$address = $row[0].', '.$row[1].', '.$row[2].', '.$row[3];
}
else {
$address = $row[0].', '.$row[1].', '.$row[2].', '.$row[3].', '.$row[4];
}
include("maps.php");
?>
****HTML****
<!DOCTYPE html>
<html>
<head>
<title>Google Maps</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style>
#map {
height: 50%;
width: 30%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input id="address" type="hidden" value="<?php echo $address; ?>">
<input id="submit" type="hidden" value="Geocode">
<div id="map"></div>
<script src="geo.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap">
</script>
</body>
</html>
****JS****
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: 51.297, lng: 22.886}
});
var geocoder = new google.maps.Geocoder();
geocodeAddress(geocoder, map);
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
var infowindow = new google.maps.InfoWindow({
content: document.getElementById('address').value,
maxWidth: 300
});
marker.addListener("click", function() {
infowindow.open(map, marker);
});
}
});
}