Question

I have a google map script like this which is the standard google map script provided in JQuery forum some where

<script type="text/javascript">          
/*
 * Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
 * Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/
$( document ).on( "pageinit", "#map-page", function() {
    <?php $coordenadas=explode(',',$fila['Googlemap']);?>
    var defaultLatLng = new google.maps.LatLng(<?php echo $coordenadas[0];?>,<?php echo $coordenadas[1];?> ); 
    drawMap(defaultLatLng); // Default to Hollywood, CA when no geolocation support

    function drawMap(latlng) {
        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
        // Add an overlay to the map of current lat/lng
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "Greetings!"
        });
    }
}); 
</script>

If you see in the code I am trying to pass longitude and latitude values extracted from a php variable to the function draw_map(); . But I am not able to do that. Can someone please help me with this ?

Edit-1

Now the map is working with the solution provided by @sukhwinder but another problem is that the map is not centered to the screen . When I open in the mobile browser the marking is shown in the top left corner and the map is not visible totally .

Was it helpful?

Solution

Because your values are not enclosed with single quotes.

Corrected code:

var defaultLatLng = new google.maps.LatLng('<?php echo $coordenadas[0];?>','<?php echo $coordenadas[1];?>');

OTHER TIPS

I'm not sure what you exactly want to do but if you want to pass lat/ long values to a PHP page,

  var center = map.getCenter();
  var centerLat = center.lat();
  var centerLong = center.lng();

Then, as jon mentioned before, you can set the values to hidden fields. Or you can simply add the values to a query string. e.g. http://foo.com/your_code.php?centerLat=...&centerLong=...

When the user submits the form, your PHP codes get the lat/long values like

<?php
  $centerLat = $_REQUEST['centerLat'];
  $centerLong = $_REQUEST['centerLong'];

  // Store the values in your database.
?>

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top