Squexis
Squexis

Reputation: 97

Changing latitude and longitude google maps api

I would like to change the latitude and longitude of the map when the user click in a link. I have tried to required maps.js when the user clicks on the link but it did not work

maps.js

function maps(){
  lats = 53.430967;
  longs = -2.960835;
  var mapProp = {
    center:new google.maps.LatLng(lats, longs),
    zoom: 17,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
}


module.exports = maps;

stadium.js

function stadium(){
  $( "a:contains('Stadium of Light')" ).on("click", function(){
    lats = 54.914740;
    longs = -1.388371;
    google.maps.event.addDomListener(window, 'load', maps);
    window.location.href='maps.html';
  });

Upvotes: 1

Views: 6580

Answers (1)

geocodezip
geocodezip

Reputation: 161324

You need to set the center property of the google.maps.Map object.

function stadium() {
  $("a:contains('Stadium of Light')").on("click", function() {
    lats = 54.914740;
    longs = -1.388371;
    map.setCenter(new google.maps.LatLng(lats,longs));
  });
}

proof of concept fiddle

code snippet:

var map;

function maps() {
  lats = 53.430967;
  longs = -2.960835;
  var mapProp = {
    center: new google.maps.LatLng(lats, longs),
    zoom: 17,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
  stadium();
}

function stadium() {
  $("a:contains('Stadium of Light')").on("click", function() {
    lats = 54.914740;
    longs = -1.388371;
    map.setCenter(new google.maps.LatLng(lats, longs));
  });
}
google.maps.event.addDomListener(window, "load", maps);
html,
body,
#googleMap {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">Stadium of Light</a>
<div id="googleMap"></div>

Upvotes: 5

Related Questions