1

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 .

2 Answers 2

2

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];?>');
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks its working but I am facing a problem here that is when I use the default longitude /latitude which is LA (hollywood). In the mobile browser the map is centered properly . But When I give my own longitude /latitude and I open in the mobile browser it shows only top left corner of mobile do you know the reason ??
var latLng = marker.getPosition(); // returns LatLng object map.setCenter(latLng); // setCenter takes a LatLng object
Can you explain more please I could not get it . Sorry I am new to all this
Its simple please go for google Maps APi, you are centering the map. Get exact location of the marker in the first line using marker.getPosition() and then set center using setCenter()
1

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.

1 Comment

I already have a field in database with longitude/latitude values which I am extracting along with other details of the page and using it to display. In short its a house rental/selling page where I am showing the location of the add

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.