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 9218451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:52:01+00:00 2026-06-18T02:52:01+00:00

I am trying to use Google Maps API to create an array of latitudes

  • 0

I am trying to use Google Maps API to create an array of latitudes and longitudes (from a mySQL database of cities) that I can use as markers.

I am unsure of how to dynamically pull the cities in PHP and geocode them in javascript.

This is what I have so far. I know the geocoder line is incorrect, but I am not sure how to work it in. Any advice or solutions would be very helpful.

  var geocoder = google.maps.Geocoder();

    var markers = [

    <?php

    //orgnize fans by city
    $query = "SELECT city, state, COUNT(*) fans FROM users GROUP BY city ORDER BY fans DESC";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){

    //pulls the city, state code from the database and stores it as a string in $address
    $address = '"' . $row['city'] . ", " . $row['state'] . '"';


       //  "$row['city'].", ".$row['state']";


        geocoder.getLatLng($address, function(point) {
                           $latitude = point.y;
                           $longitude = point.x;
        });
        echo "{ lat: \"".$latitude.", lng: ".$longitude.", name: "'"'.$row['city'].", ".$row['state'].'"'"},\n ";
        echo "<br/>";
    }

    ?>

    ];

    //var locationNameArray = ['Portland','Seattle','Los Angeles','San Diego','Las Vegas'];

    // Create the markers ad infowindows.
    for (index in markers) addMarker(markers[index]);
    function addMarker(data) {
        // Create the marker
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.lng),
            map: map,
            title: data.name
        });
  • 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-18T02:52:02+00:00Added an answer on June 18, 2026 at 2:52 am

    In your post you are mixing PHP with Javascript.
    You can’t use a Javascript function geocoder.getLatLng inside a PHP tag.

    To fill the database with lat, lng information I suggest to use the geocoding with PHP.
    So you will need one table for cities.

    Table city:
    
    id_city
    name
    state
    lat
    lng
    

    And the user table have just a foreign key to id_city:

    Table users:
    
    id_user
    fi_city
    ...
    

    And here is a quick and dirty example how to get the lat, lng information from Google geocoder with PHP:

    $address = urlencode('New York');
    $googleApi = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';
    
    $json = file_get_contents(sprintf($googleApi, $address));
    
    $resultObject = json_decode($json);
    
    $location = $resultObject->results[0]->geometry->location;
    
    $lat = $location->lat;
    $lng = $location->lng;
    
    echo 'Lat: '.$lat.', Lng: '.$lng;
    

    There is no error checking but I hope you get the point.
    With this result you can update your database.
    I hope you know how to join multiple tables to get the result from users and city tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the Google Maps API v2 in an android project that
I'm trying to use the Google Maps V3 API to create markers on a
I am trying to use google maps API V3 to draw a line from
I'm trying to use Google Analytics API to query internal searches that happen on
I am trying to create a Google Maps application that has the map of
I am trying to use my custom styling created through Google Maps API v3
I'm experiencing a problem while trying to use google maps api v3 (with marker
I was trying to use the Google Maps API V3 in my new Windows
GOOGLE MAPS API V3 is what i'm trying to use. I have all the
I am trying to use the Google maps API on my android app. I

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.