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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:47:16+00:00 2026-05-28T02:47:16+00:00

I am trying to display a Google map when a user searches for a

  • 0

I am trying to display a Google map when a user searches for a city and presses the search button, but I do not understand how to do it properly. Here is my code done so far. I don’t get how to take user input and check that city is in the array. If it is in the array then it should display it on the Google map. For example, if the user types the city name Houston, USA then it should display on the Google map after checking whether the city name is in our database.

Script:

function searchResult(cityname) { 
    var namesOfCity = ["houston,boston,newyork,florida"];

    //check the user input 

    //if match display it on google map    
}

function initialize() 
{
        if(GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(37.4419, -122.1419), 13);
            map.setUIToDefault();
        }
    }

HTML

<body onload="initialize()" onunload="GUnload()">


 <input type="text" id="cityname" value="" name=""/>
  <input id="search" type="button" value="Search" onclick="searchResult('cityname')" />
  <div id="map" style="width: 100%; height: 450px; position: relative; background-color: rgb(229, 227, 223);"></div>

</body>
  • 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-05-28T02:47:17+00:00Added an answer on May 28, 2026 at 2:47 am

    You are close, but you are not initialising your array correctly.

    You are also not storing the correct coordinates for each city in your array.

    Instead, try using a custom object to store the names of allowed cities and their coordinates (latitude and longitude) and looking at that to determine what to display:

    function searchResult(cityname) { 
        var cities = {
            houston: { 
               lat: /* ... */,
               long: /* ... */
            },
            boston: { 
               lat: /* ... */,
               long: /* ... */
            },
            'new york': { // note the quotes due to a space in the name
               lat: /* ... */,
               long: /* ... */
            },
            florida: { 
               lat: /* ... */,
               long: /* ... */
            }
        };
    
        //check the user input 
        var textfield = document.getElementById(cityname);
    
        if (textfield) {
            // look up the lowercase version of the value typed in
            var coords = cities[textfield.value.toLowerCase()];
            //if match display it on google map   
            if (coords) {
                var map = new GMap2(document.getElementById("map"));
                map.setCenter(new GLatLng(coords.lat, coords.long), 13); // you could also make the zoom level a property of each city, which would allow you to customise that per city
                map.setUIToDefault();
            }
        }     
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am trying to dispaly a basic google map on my application but that's not
I'm trying to display a Google map from within a WebWorks app. Here is
I'm trying to display a Google Map inside a drupal page. The map will
I am trying to display google map as a tooltip using qTip jquery plugin.
I'm trying to transfer Rails-generated json into Google Maps markers on a map, but
I'm trying to set up a Google map that will display when a link
I am trying to use JSNI to display a Google Map within an Application.
i am trying to display google map on Android refering to this tutorial http://developer.android.com/resources/tutorials/views/hello-mapview.html
I'm trying to display a google map on a webpage. The address that the
I'm trying to display a sidebar on the left side of a google map.

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.