Avatar of APD Toronto
APD TorontoFlag for Canada

asked on 

Setting a Central Location in Google Maps

Hi Experts,

I am just starting out with Google Maps API, and every example that I see starts off with a static central location, like Toronto that I have below.

My question is, do I really need a central location, because I dont know where that center will be. Toronto is in Ontario, but the user may want to see stores in BC. I am still working towards preparing the longitude/latitude, but I just thinking about my next step.

function get_map(){

    var toronto = {lat: 43.6560531, lng: -79.3869317};
    map = new google.maps.Map(document.getElementById('results_map'), {
      zoom: 12,
      center: toronto
    });
    
    geocoder = new google.maps.Geocoder();
  
}

Open in new window


Thank you
GoogleJavaScript

Avatar of undefined
Last Comment
APD Toronto
Avatar of lenamtl
lenamtl
Flag of Canada image

Hi,

Maybe you can display empty map then fill it based on the user search (select a city into the list and this city is related to lat/long that will be use for the center)...
or you can detect user lat / long (if the browser setting allow it) if not you may add fields so use can enter their lat/long
or you can display a map zoom out, so user can see the entire province or country...

You can use LatLngBounds https://developers.google.com/maps/documentation/javascript/reference?csw=1#LatLngBounds
ASKER CERTIFIED SOLUTION
Avatar of APD Toronto
APD Toronto
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo