Retrieve values from mssql to add new markers to Google Maps
Can anybody help me with this code... Basically I want to output a Google map with marker locations from my MSSQL databse. I am connecting to the database and outputting data from the database with out any issues. The values in my database that need to be used are latitude and longitude
I just need some help getting the latitude and longitude information from the database into an array that can feed through to "var markers = createMarker". Can anyone suggest any code or advice?
<!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</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=true_or_false&key=ABQIAAAAFdAFQ5eUeLToBjop7wxvVRQwhZZS-c1L0GQSo2zewspgIUCbcBQLESvgR7pk_2Z2FMWsfROoCbyJrw" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(
document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.addMapType(G_SATELLITE_3D_MAP);
map.setCenter(
new GLatLng(37.4419, -122.1419), 13);
function createMarker(point, text, title) {
var marker =
new GMarker(point,{title:title});
return marker;
}
<?php
$points = Array(
"37.4389, -122.1389",
"37.4419, -122.1419",
"37.4449, -122.1449");
foreach ($points as $key => $point) {
?>
<!-- ADD NEW MARKERS FROM A DATABASE -->
var marker = createMarker(
new GLatLng(<?php echo $point ?>)
);
map.addOverlay(marker);
<?php } ?>
}
}
//]]>
</script>
</head>
<body onload="load()" onunload="GUnload()">
<div id="map"
style="width: 500px; height: 300px"></div>
<?php
/*data base connection */
/* SQL query */
$tsql = ("SELECT gps_lat
,gps_long
FROM location");
$result = sqlsrv_query($conn,$tsql);
if( $result === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/*Display the table */
// keeps getting the next row until there are no more to get
while($points = sqlsrv_fetch_array( $result )) {
echo $points['gps_lat'];
echo ',';
echo $points['gps_long'];
echo "</br>";
}
/* Close the connection. */
sqlsrv_close( $conn);
?>
</body>
</html>
I have tried to create the new markers by using the below method, but I have been unsuccessful. Can anyone point out anything wrong with the code below or give me any advice?
//<![CDATA[
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(
document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.addMap开发者_JS百科Type(G_SATELLITE_3D_MAP);
map.setCenter(
new GLatLng(37.4419, -122.1419), 13);
function createMarker(point, text, title) {
var marker =
new GMarker(point,{title:title});
return marker;
}
<?php
/*data base connection */
/* Connect using Windows Authentication */
$conn = sqlsrv_connect($serverName, $connectionOptions);
/* Check whether connnection is established */
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
/* SQL query */
$tsql = ("SELECT TOP 10 gps_lat
,gps_long
FROM location");
$result = sqlsrv_query($conn,$tsql);
if( $result === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/*
BELOW IS THE ADJUSTED WHILE LOOP
*/
while($row = sqlsrv_fetch_array( $result )) {
$lat = $row['gps_lat'];
$long = $row['gps_long'];
?>
var marker = createMarker(
new GLatLng(<?php echo $Lat.','.$Long;?>)
);
map.addOverlay(marker);
<?php } ?>
}
}
//]]>
</script>
I thought it would be fairly straightforward but I can't seem to get my head around it. If there is any information that is unclear...
I did actually manage to resolve the issue in the end by working through the night, trying loads of different things... The code for retrieving the values are as follows.
$lat=array();
$long=array();
while($points = sqlsrv_fetch_array($result)) {
$lat[] = $points['gps_lat'];
$long[] = $points['gps_long'];
}
$latLongSize = sizeof($lat)-1;
for($i=0; $i<=$latLongSize; $i++){ ?>
var marker<?php echo $i; ?> = createMarker(new GLatLng(<?php echo $lat[$i];?>, <?php echo $long[$i]; ?>));
map.addOverlay(marker<?php echo $i; ?>);
精彩评论