Jump to content

php/javascript problem with google maps


garry27

Recommended Posts

i've got a web page which calls on two scripts to deliver it a google map containing markers as defined by coordinates on  my database.

the first script, map_data.php, pulls out these co-ordinates and formats it in a javascript data structure. it then outputs the js back to the browser.

the second is a js script which contains instructions to pass on to google in order for it to pass the map to my browser. this includes a reference to the markers variable containing the data required for the markers (longitude and longitude, as well as some optional values).

the problem is that my web app isn't working as described. currently it pulls up the map but my markers are absent. my debugger tells me that 'markers is undefined' on line 23 and there's no js when i right mouse click soure on the page (although i'm not sure if there's meant to be).

the code for both scripts is shown below. can someone look at it and tell me what's wrong with it. thanks


[b]map_data.php[/b]
[code]<?php           
function getPubMarkers() {
    require_once('common.php');
    db_connect();
    $sql = mysql_query ( " select Latitude, Longitude, pubName, pubStreetNo, pubStreet, pubCity, pubCounty,
                        pubPostcode, pubPhone from Pubs " ) or die( 'Invalid query: ' . mysql_error() );
   
    while( $elements = mysql_fetch_array($sql) ) {
$br = '<br>';     
    $markers[] =      '{' 
                        ." 'latitude' : " .$elements[0] .','
." 'longitude' : " .$elements[1] .','
                        ." 'name' : '" .$elements[2] ."' ,"
                    //    ."'address': '" .$elements[3] .$br .$elements[4] .$br .$elements[5] 
                    //                    .$br .$elements[6] .$br .$elements[7] ."' ,"
                    //    ."'phone': '" .$elements[8] ."'"
      .'},';
    }

    //convert array into a single string variable
    $markers  = implode( ' ', $markers );
    //remove the end comma in the string
    $markers = substr_replace($markers,"",-1);
   
    $jscript = 'var markers =' .'[' .$markers .'];';
    return $jscript;
}
?>[/code]


[b]map_js.js[/b]
[code]var map;
var centerLatitude = 54.524842;        // global var
var centerLongitude = 1.556604;        // global var
var startZoom = 3;

//function addMarker()
function init()
{ // JavaScript coding from Purvis et al (2006: p.22, p.29)
  if ( GBrowserIsCompatible() ) {                     
    map = new GMap2(document.getElementById("mapviewer"));
    map.addControl( new GSmallMapControl () );
    //map.addControl( new GMap2TypeControl () );          //undefined?
    //map.addControl( new GLatLng ( centerLatitude, centreLongitude ), startZoom );
//location = GLatLng(centerLatitude, centerLongitude);
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    for ( id in markers ) {                                                                                          //line 24
  addMarker( markers[id].latitude, markers[id].longitude, markers[id].name ); 
[id].address, markers[id].phone
    }
  }
}

//opens map when browser window loads up
window.onload = init;
window.onunload = GUnload;[/code]
Link to comment
https://forums.phpfreaks.com/topic/26216-phpjavascript-problem-with-google-maps/
Share on other sites

Somewhere in the Javascript portion of your script there needs to be a line that looks something like:
[code]<?php
echo getPubMarkers();
?>[/code]
Without that line, the markers never get defined in Javascript.

Also, I would re-write the getPubMarkers function a little to avoid the problem with the trailing comma:
[code]<?php
function getPubMarkers() {
    require_once('common.php');
    db_connect();
    $sql = mysql_query ( " select Latitude, Longitude, pubName, pubStreetNo, pubStreet, pubCity, pubCounty,
                        pubPostcode, pubPhone from Pubs " ) or die( 'Invalid query: ' . mysql_error() );
    $markers = array();
    while( $elements = mysql_fetch_assoc($sql) ) {
        $tmp = array();
        foreach($elements as $field => $val) {
            switch ($key) {
                  case 'Latitude':
                  case 'Longitude':
                      $tmp[] = " '" . strtolower($field) . "' : " . $val;
                      break;
                  case 'pubName':
                  case 'pubPhone':
                      $tmp[] = " '" . strtolower(str_replace('Pub','',$key)) . "' : '" . $val . "'";
                      break;
                  case 'pubStreet':
                      $tmp[] = " 'address' : '" . $val . '<br>' . $elements['pubStreet'] . '<br>' . $elements['pubCity'] . '<br>' . $elements['pubCounty'] . '<br>' . $elements['pubPostCode'] . "'";
                      break;
            }
        }
        $markers[] = '{' . implode(',',$tmp) . '}';
    }

    //convert array into a single string variable
    $markers  = implode( ' ', $markers );
    $jscript = 'var markers =' .'[' . implode(',',$markers) .'];';
    return $jscript;
}
?>[/code]

Ken
i now get 'object undefined on  line 24' but no 'markers undefined' in my javascript:


[code]function init()
{ // JavaScript coding from Purvis et al (2006: p.22, p.29)
  if ( GBrowserIsCompatible() ) {                     
    map = new GMap2(document.getElementById("mapviewer"));
    map.addControl( new GSmallMapControl () );
    //map.addControl( new GMap2TypeControl () );          //undefined?
    //map.addControl( new GLatLng ( centerLatitude, centreLongitude ), startZoom );
//location = GLatLng(centerLatitude, centerLongitude);
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    for ( id in markers ) {
  addMarker( markers[id].latitude, markers[id].longitude, markers[id].name );  // line 24
    }
  }
}[/code]

???
ken, when i try it your way, is still get 'markers undefined' and a bunch of 'undefined index errors' :

[quote]var markers =[{ 'latitude' : 54.5269, 'longitude' : -1.55736, 'pubname' : '2', 'address' : '2<br>2<br>2<br>2<br>', 'pubphone' : '2'},{ 'latitude' : 54.5269, 'longitude' : -1.55736, 'pubname' : 'Aruba', 'address' : 'King St<br>King St<br>Darlington<br>County Durham<br>', 'pubphone' : '1325'},{ 'latitude' : 54.5234, 'longitude' : -1.55736, 'pubname' : 'Berlins', 'address' : 'Grange Rd<br>Grange Rd<br>Darlington<br>County Durham<br>', 'pubphone' : '1325'},{ 'latitude' : 54.5239, 'longitude' : -1.55736, 'pubname' : 'Flares', 'address' : 'Skinnergate<br>Skinnergate<br>Darlington<br>County Durham<br>', 'pubphone' : '1325'},{ 'latitude' : 54.5236, 'longitude' : -1.55736, 'pubname' : 'Humphrys', 'address' : 'Blackwellgate<br>Blackwellgate<br>Darlington<br>County Durham<br>', 'pubphone' : '1325'},{ 'latitude' : 54.5263, 'longitude' : -1.55736, 'pubname' : 'The Turks Head', 'address' : 'Bondgate<br>Bondgate<br>Darlington<br>County Durham<br>', 'pubphone' : '1325'},{ 'latitude' : 54.5254, 'longitude' : -1.55736, 'pubname' : 'Tanners Hall', 'address' : 'Skinnergate<br>Skinnergate<br>Darlington<br>County Durham<br>', 'pubphone' : '0'},{ 'latitude' : 54.5246, 'longitude' : -1.55355, 'pubname' : 'The Boot And Shoe', 'address' : 'Church Row<br>Church Row<br>Darlington<br>County Durham<br>', 'pubphone' : '1325'},{ 'latitude' : 54.5256, 'longitude' : -1.56079, 'pubname' : 'Atlantic', 'address' : 'Duke Street<br>Duke Street<br>Darlington<br>Co. Durham<br>', 'pubphone' : '1325'}];  [/quote]

[code]$markers = array();

while( $elements = mysql_fetch_assoc($sql) ) {
        $tmp = array();
        foreach($elements as $field => $val) {
            switch ($field) {
                  case 'Latitude':
                  case 'Longitude':
                      $tmp[] = " '" . strtolower($field) . "' : " . $val;
                      break;
                  case 'pubName':
                  case 'pubPhone':
                      $tmp[] = " '" . strtolower(str_replace('Pub','',$field)) . "' : '" . $val . "'";
                      break;
                  case 'pubStreet':
                      $tmp[] = " 'address' : '" . $val . '<br>' . $elements['pubStreet'] . '<br>' . $elements['pubCity'] . '<br>' . $elements['pubCounty'] . '<br>' . $elements['pubPostCode'] . "'";
                      break;
            }
        }
        $markers[] = '{' . implode(',',$tmp) . '}';
    }

    //convert array into a single string variable
    //$markers  = implode( ' ', $markers );
    $jscript = 'var markers =' .'[' . implode(',',$markers) .'];';
    return $jscript;[/code]

but when i code it my way, i get 'object expected':

[code]$markers = array();

    while( $elements = mysql_fetch_array($sql) ) {
$br = '<br>';     
    $markers[] =      '{' 
                        ." 'latitude' : " .$elements[0] .','
." 'longitude' : " .$elements[1] .','
                        ." 'name' : '" .$elements[2] .",' "
                        //."'address': '" .$elements[3] .$br .$elements[4] .$br .$elements[5] 
                        //                .$br .$elements[6] .$br .$elements[7] ."' ,"
                        // ."'phone': '" .$elements[8] ."'"
      .'},';
    }

    //convert array into a single string variable
    $markers  = implode( ' ', $markers );
    //remove the end comma in the string
    $markers = substr_replace($markers,"",-1);
   
$jscript = 'var markers =' .'[' .$markers .'];'; 
return $jscript; [/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.