Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:45:05+00:00 2026-06-05T01:45:05+00:00

I am using Google Maps API v3. My script works fine and shows a

  • 0

I am using Google Maps API v3. My script works fine and shows a car icon as long as
the marker is undefined. But when I update long lat in the database and the ajax function returns a new long lat via json, marker.setPosition(newLatLng) makes the marker disappear…

Please, help me out, what is the problem? Here is my code:

 $(document).ready(function () {
    var map;
    var marker;
    function initialize() {
        var myLatlng = new google.maps.LatLng(<?php echo $c_lat; ?>, <?php echo $c_long; ?>);
        var myOptions = {
            zoom: 19,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        map = new google.maps.Map(document.getElementById('driver_loc'), myOptions);
    }
function getLongLat(){
     $.ajax({
           type: "POST",
           url: "config/getLongLat.php?id=<?php echo $driverId; ?>",
           dataType: "json",
           success: function(data)
                {   
            var lat=data.lat; //returns new lat via db
            var lon=data.lon;
                // alert(lat);   
           var newLatLng = new google.maps.LatLng(lat, lon);

            if (marker != undefined)
                marker.setPosition(newLatLng);
            else
                marker = new google.maps.Marker({
                    position: newLatLng,
                    map: map,   
                    icon: 'images/car.png',
                    animation: google.maps.Animation.DROP, 
                    draggable: true
                });
            }
        });
        console.log(marker);
    }
    // Onload handler to fire off the app.
    google.maps.event.addDomListener(window, 'load', initialize);
  //  google.maps.event.addDomListener(window, 'load', callPosition);
    window.setInterval(function(){
  getLongLat();
}, 6326);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:45:06+00:00Added an answer on June 5, 2026 at 1:45 am

    A good practice is to use parseFloat method for json data. A new position can be outside of current view port. To handle it, just use map.setCenter([new_position]) method. You can also change zoom level with map.setZoom([zoom_level]).
    In your case:

      $(document).ready(function () {
        var map;
        var marker;
        function initialize() {
          var myLatlng = new google.maps.LatLng(<?php echo $c_lat; ?>, <?php echo $c_long; ?>);
          var myOptions = {
            zoom: 19,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }
          map = new google.maps.Map(document.getElementById('driver_loc'), myOptions);
        }
        function getLongLat(){
          $.ajax({ type: "POST", url: "config/getLongLat.php?id=<?php echo $driverId; ?>", dataType: "json",
                   success: function(data){   
                     var lat=parseFloat(data.lat); //returns new lat via db
                     var lon=parseFloat(data.lon);
                     var newLatLng = new google.maps.LatLng(lat, lon);
    
                     if (marker != undefined){
                       marker.setPosition(newLatLng);
                     } else {
                       marker = new google.maps.Marker({
                         position: newLatLng,
                         map: map,   
                         icon: 'images/car.png',
                         animation: google.maps.Animation.DROP, 
                         draggable: true
                       });
                     }
                     //Center map on a new position
                     map.setCenter(newLatLng);
                     //Change zoom level
                     //map.setZoom(19);
                   }
          });
        }
        // Onload handler to fire off the app.
        google.maps.event.addDomListener(window, 'load', initialize);
        //  google.maps.event.addDomListener(window, 'load', callPosition);
        window.setInterval(function(){
          getLongLat();
        }, 6326);
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Google Maps API V3 to create a marker and the Infobox
Update II I Figured this out. When using google maps api, the model must
I'm using the Google Maps API V3 and I'm trying to make a marker
I am using google maps on my web site. It works perfectly fine in
i've tried using google maps marker manager, but i seem to be hitting a
I am using Google Maps API V2 (I know, but i am editing a
I just started working using Google Maps API yesterday, and trying to set up
All the examples I've come across using Google Maps API seem to show a
I have created an infobox using google maps api v3 and infoBox from the
I want to add some custom roads using google maps api (i.e. off-road routes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.