We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10596
    • 18 Posts
    Hi folks,
    I managed to create a successful snippet to display a static google map with some pin markers. You need TV (text input) for latitude and longitude. This hasnt been a package. The following snippet code is working, BUT...it is not the best written code since I write the output directly from the snippet and it infringes the logic ModX flow (it should be by chunk template). The snippet works by comparing a single latitude&longitude with others, and it needs getResourceField package by http://modx.com/extras/author/paulmerchant

    Be free to modify the code. I shall not publish the working url since it's still on the development server.
    Comments are welcome.
    ====================
    Snippet name: getNearby
    Call example:
     [[!getNearby? &latlng=`[[*googlemaps]]` &frmpoint=`[[*id]]`]]

    TVs: * googlemaps: text input: eg: -6.25713, 106.85555
    ===================

    <?php
    include(MODX_CORE_PATH.MODX_CONFIG_KEY.'/'.MODX_CONFIG_KEY.'.inc.php');
    try {
        $xpdo = new xPDO("mysql:host=$database_server;dbname=$dbase", $database_user, $database_password);
        /*** echo a message saying we have connected ***/
      //  echo 'Connected to database yes 
    ';
      //echo 'nearby places from ID'.$frmpoint. ' are:
    ';
      /**explode character from**/
      $piecescoord = explode(",", $latlng);
      $sql = "SELECT id, contentid, lat, lng, 
    (6371 * ACOS( COS( RADIANS( $piecescoord[0] ) ) * COS( RADIANS( lat ) ) * COS( RADIANS(lng ) - RADIANS( $piecescoord[1] ) ) + SIN( RADIANS( $piecescoord[0] ) ) * SIN( RADIANS( lat ) ) )) AS distance
    FROM (SELECT id, contentid, SUBSTRING_INDEX(value,  ',', 1 ) AS lat, SUBSTRING_INDEX(value,  ',', -1 ) AS lng
    FROM modxhojkt_site_tmplvar_contentvalues WHERE tmplvarid=10) AS TEMPORARY 
    HAVING distance > 0 AND distance < 5
    ORDER BY distance
    LIMIT 0 , 9";
    foreach ($xpdo->query($sql) as $row)
            {
    	$contentid[]=$row['contentid'];
    	$distance[]=$row['distance'];
    	}
      $countnearby=count($contentid); 
      for ($i = 0; $i <= $countnearby-1; $i++) {
      $longtitleOutput = $modx->runSnippet( 'getResourceField', array(
               'elementClass' => 'modSnippet', 
               'element' => 'getResourceField',
               'id' => $contentid[$i],
               'field' => 'longtitle'
           )
       );
        $placetypeOutput = $modx->runSnippet( 'getResourceField', array(
               'elementClass' => 'modSnippet', 
               'element' => 'getResourceField',
               'isTV' => 'true',
               'processTV' => 'true',
               'id' => $contentid[$i],
               'field' => 'places'
           )
       );
        $latlngOutput = $modx->runSnippet( 'getResourceField', array(
               'elementClass' => 'modSnippet', 
               'element' => 'getResourceField',
               'isTV' => 'true',
               'processTV' => 'true',
               'id' => $contentid[$i],
               'field' => 'googlemaps'
           )
       );
        print '<div class="left ranking">'.($i+1).'</div><li><a href="'.$url = $modx->makeUrl($contentid[$i]).'"><span class="bold">'.$placetypeOutput.'</span>'.$longtitleOutput.'</a></li>';
        $latmarkers[]=$latlngOutput;
        $labelmarkers[]=$i+1;
        $markers.='|&markers=color:0x8EBD00|size:mid|label:'.$labelmarkers[$i].'|'.$latmarkers[$i];
      }
      echo '<div class="clear"></div><div class="big-spacer"></div><div class="aligncenter">';
      echo '<img src="http://maps.google.com/maps/api/staticmap?center='.$latlng.'&markers=color:blue|label:o|'.$latlng.$markers.'&zoom=12&size=300x200&sensor=false" /></a>'; 
      echo '</div>';
    //echo $markers;
    //echo '
     there are '.$countnearby.' nearby point';
        /*** close db connection ***/      
        $xpdo = null;
        }
    catch(PDOException $e)
        {
        echo $e->getMessage();
        }


    [ed. note: gee last edited this post 12 years, 6 months ago.]