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

  • SEARCH
  • Home
  • 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 8066761
In Process

The Archive Base Latest Questions

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

I am using a google maps API 3 for my application. So what I

  • 0

I am using a google maps API 3 for my application.

So what I want to do is next.
When page is loading I will get an array of positions in this format:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

Firs I want to add marker to each of this positins

var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

Picture that goes on the marker:

var image = new google.maps.MarkerImage("images/truck3.png",
        new google.maps.Size(32.0, 37.0),
        new google.maps.Point(0, 0),
        new google.maps.Point(16.0, 18.0)
    );
  var shadow = new google.maps.MarkerImage("images/shadow-truck3.png",
        new google.maps.Size(51.0, 37.0),
        new google.maps.Point(0, 0),
        new google.maps.Point(16.0, 18.0)
    );

  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map,
    icon: image,
    shadow: shadow,
    title: 'Click to zoom'
  });

And this should zoom all the markers that are initialized in the center of the screen but only once at the beginning later on center is not used as user may fish to look something else on the map.

I want a markers to be moving all the time so maybe I need to add some event handler to fetch new position all the time?

Then comes the part when you click on the marker, you got zoomed and marker is still moving ( getting new position ) but now I need to use that ID that I will send in the php array so I can call some php function with that parameter.

After that when user clicks on the button which is on the side I want to be able to come at the beginning of my request and that means loaded markers in the center of the screen.

Part with the click:

 google.maps.event.addListener(marker, 'click', function() {
    map.setZoom(13);
    map.setCenter(marker.getPosition());
    google.maps.event.addListener(map, 'center_changed', function() {
    // 1 seconds after the center of the map has changed, pan back to the
    // marker.
    window.setTimeout(function() {
      map.panTo(marker.getPosition());
    }, 1000);
  });
  });
}

So basically I told you everything that I want to achieve I am sure that a lot of you made the same thing. These are part of the code from a lot of sources I need to make them work together and add some smaller new features as I wrote above.

Thank you

  • 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-05T12:06:10+00:00Added an answer on June 5, 2026 at 12:06 pm

    So it sounds like you are retrieving the locations variable with a database query repeated every few seconds. In this demo I’m using JSON to retrieve the data, and update the location or create the marker if it doesn’t already exist.

    http://freezoo.alwaysdata.net/truck2.html

        function updateTrucks() {
          $.ajax({
            url: "gettruck.php",
            cache: false,
            dataType: 'json',
            data: {},
            error: function(jqxhr, textStatus, errorThrown) {
              alert("Error: " + textStatus + " " + errorThrown);
            },
            success: function(data) {
            for(var i = 0; i < data.results.length; i++) {
              if(trucks.hasOwnProperty(data.results[i].id)) {
                // update position
                trucks[data.results[i].id].setPosition(new google.maps.LatLng(
                  parseFloat(data.results[i].lat),
                  parseFloat(data.results[i].lng)));
                trucks[data.results[i].id].speed = data.results[i].speed;
              }
              else {
                // create new marker
                trucks[data.results[i].id] = new google.maps.Marker({
                  map: map,
                  position: new google.maps.LatLng(
                    parseFloat(data.results[i].lat),
                    parseFloat(data.results[i].lng)),
                  title: data.results[i].name,
                  id: data.results[i].id
                });
    

    In your application the database will get updated by some means, here, the database is static and movement (for the demo) is artificially created with PHP rand, so every few seconds the markers appear in different locations.

    header("Content-type: application/json");
    echo '{ "results" : [ ';
    
    $gettruck_result = $dbh->query("SELECT * FROM `truck`");
    $result_array = array();
    
    foreach($gettruck_result as $row) {
      $newlat = $row['lat'] + (0.0025 * rand(-3,3));
      $newlng = $row['lng'] + (0.0025 * rand(-3,3));
    
      $speed = $row['speed'] + rand(1,8);
    
      $row_object = '{';
      $row_object .= '"id": "' . $row['id'] . '", ';
      $row_object .= '"speed": "' . $speed . '", ';
      $row_object .= '"name": "' . $row['name'] . '", ';
      $row_object .= '"lat": "' . $newlat . '", ';
      $row_object .= '"lng": "' . $newlng . '"';
      $row_object .= '}';    
      $result_array[] = $row_object;
    }
    
    $result_str = implode(", ", $result_array);
    echo $result_str;
    echo " ] }";
    

    Tracking a marker is done with two listeners:

            google.maps.event.addListener(trucks[data.results[i].id], 'position_changed', function() {
              if(typeof tracked !== "undefined") {
                if(this.id == tracked.id) {
                  map.panTo(this.getPosition());
                  $("#speed").val(this.speed);
                } 
              } 
            });         
    
            google.maps.event.addListener(trucks[data.results[i].id], 'click', function() {
              $("#trackedId").val(this.id);
              map.setZoom(13);
              map.setCenter(this.getPosition());
              tracked = this;
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Google Maps API on my ASP.NET Web Application. Currently the center position
We're using the Google Maps API for an application to determine distance and driving
I have an application using the Google Maps javscript API and the Infobox plug-in
im using google maps api and im having this problem with showing the info
I'm developing a map application in Android. I'm using Google maps API to show
I am developing a gis application using Google maps API. Currently I am using
I'm working on an advanced map application in Google Maps API V3. I'm using
I am creating a small application using HTML 5, Javascript and Google Maps API
I'm writing a web application using the google maps api v3, which displays places
I am writing an android application however I am using the google maps api

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.