API di Google Maps v3 - PHP - MYSQL
-
08-07-2019 - |
Domanda
GOOGLE MAPS API V3 è ciò che sto cercando di utilizzare.
Ho tutte le coordinate in mysql. Ad esempio, devo solo prendere 5 elenchi e inserirli in una mappa, ma mi piacerebbe poter trovare il punto centrale in base alle coordinate multiple che vorrei visualizzare e anche il livello di zoom. Sì, lo sai?
Sto passando il tempo della mia vita con qualcosa che so essere terribilmente semplice, non riesco proprio a capire questa API. Pagherò $ 20 a chiunque mi possa aiutare.
//select * from mysql limit 5
//ok great we got 5 results, great job, format the 5 results so google maps like it, [name,lat,lng] whatever.
//put them on the map and let them be clickable so i can put stuff in the infowindow thing
//make the map adjust to the proper zoom level and center point
UPDATE
Questo è quello che stavo cercando, spero che questo aiuti gli altri.
credito a [Chris B] per la formula matematica di buon senso per ottenere il coord centrale, il cavo sw è il lat e lon più basso e il ne coord è il lat e lon più grande
sort($lat)&&sort($lon);
$r['c'] = array_sum($lat)/count($lat).', '.array_sum($lon)/count($lon);
$r['ne'] = $lat[count($lat)-1].', '.$lon[count($lon)-1];
$r['sw'] = $lat[0].', '.$lon[0];
var myOptions = {zoom:4,center: new google.maps.LatLng(<?php echo $r['c']; ?>),mapTypeId:google.maps.MapTypeId.ROADMAP}
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
<?php foreach($x as $l) echo 'new google.maps.Marker({position:new google.maps.LatLng('.$l['lat'].','.$l['lon'].'),map:map,clickable:true});'; ?>
map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(<?php echo $r['sw']; ?>),new google.maps.LatLng(<?php echo $r['ne']; ?>)));
Soluzione
Se un punto centrale medio / ponderato è accettabile, potresti semplicemente fare la media di tutte le latitudini e fare la media di tutte le longitudini:
$latTotal = 0;
$lngTotal = 0;
foreach ($markers as $marker) {
$latTotal += $marker['lat'];
$lngTotal += $marker['lng'];
}
$centerLat = $latTotal/count($markers);
$centerLng = $lngTotal/count($markers);
Per il resto, ci sono alcuni buoni tutorial V3 su Google .
Altri suggerimenti
Stavo usando Google Maps v3 un mese o due indietro, ma in seguito sono passato a v2. Tuttavia, ho avuto lo stesso problema, quindi ho scritto una classe MarkerManager per API v3. Non riesco a trovare l'ultima versione della mia classe, ma ne ho trovata una, si spera, funzionante. Puoi trovarlo qui .
Devo avvisarti però: non è affatto ottimizzato e non usa sovrapposizioni, quindi quando ho provato a mettere più di 50 marker nel manager e ho attivato la funzione Nascondi / Mostra, la classe è sloooow ... Ma forse puoi avere un certo successo con esso.
Esempio di utilizzo:
var map = new google.maps.Map(document.getElementById('MapLayerId'), {
zoom: 7,
position: new google.maps.LatLng(latitude, longitude),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
map: map
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
map: map
});
var manager = new MarkerManager(map, {fitBounds: true});
manager.add(marker1);
manager.add(marker2);
manager.show();
GDownloadUrl in V2 equivalente downloadUrl in GOOGLE MAPS API V3
Come caricare tutte le coordinate nel database (Mysql o Sql).
var myLatlng = new google.maps.LatLng("37.427770" ,"-122.144841");
var myOptions = {zoom: 15, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP,}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var url='marker.php?arg1=x&arg2=y...';
downloadUrl(url, function(data) {
var markers = data.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = new google.maps.Marker({position: latlng, map: map});
}
});
function createXmlHttpRequest() {
try {
if (typeof ActiveXObject != 'undefined') {
return new ActiveXObject('Microsoft.XMLHTTP');
} else if (window["XMLHttpRequest"]) {
return new XMLHttpRequest();
}
} catch (e) {
changeStatus(e);
}
return null;
};
function downloadUrl(url, callback) {
var status = -1;
var request = createXmlHttpRequest();
if (!request) {
return false;
}
request.open('GET', url);
request.onreadystatechange = function() {
if (request.readyState == 4) {
try {
status = request.status;
} catch (e) {
// Usually indicates request timed out in FF.
}
if (status == 200) {
var s=request.responseText;
callback( xmlParse(s) );
}
}
}
try {
request.send(null);
}catch (e) {
changeStatus(e);
}
};
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
Hai controllato il tutorial PHP / Mysql di google map ufficiale? http://code.google.com/apis/maps/articles/phpsqlajax. html
Prova questo algoritmo per trovare il centroide di un poligono:
http://tog.acm.org/resources/GraphicsGems/gemsiv /centroid.c