javascript - html link to relocate google map -


on personal blog website making, have background set google map , have smaller div on top talking places i've been etc. want have link "show place in google map" , when clicked, background map updated corresponding location.

i tried making javascript code

<script> function initialize() {     var mapcanvas = document.getelementbyid('map-canvas');     var mapoptions = {         scrollwheel: false,         center: new google.maps.latlng(34.0722, -118.4441),         zoom: 14,         zoomcontroloptions: {             position: google.maps.controlposition.right_center         },         streetviewcontrol: false,         pancontrol: false,         maptypecontrol: false,         maptypeid: google.maps.maptypeid.roadmap     }     var map = new google.maps.map(mapcanvas, mapoptions);      var marker = new google.maps.marker(); }  google.maps.event.adddomlistener(window, 'load', initialize); </script>  <script type="text/javascript">     window.onload = function() {         var = document.getelementsbyclassname("map-link");         a.onclick = function() {             marker.setmap(map);         }     } </script>  <a class="map-link" href="javascript:void(0)"> show in google maps </a> 

something this, i'm not seeing result. know how go this?

  1. use map.setcenter change center of map.
  2. getelementsbyclassname returns array of dom elements.

    google.maps.event.adddomlistener(document.getelementsbyclassname("map-link")[0], 'click', function (e) { map.setcenter(new google.maps.latlng(42, -72)); });

proof of concept fiddle

code snippet:

function initialize() {    var mapcanvas = document.getelementbyid('map-canvas');    var mapoptions = {      scrollwheel: false,      center: new google.maps.latlng(34.0722, -118.4441),      zoom: 14,      zoomcontroloptions: {        position: google.maps.controlposition.right_center      },      streetviewcontrol: false,      pancontrol: false,      maptypecontrol: false,      maptypeid: google.maps.maptypeid.roadmap    }    var map = new google.maps.map(mapcanvas, mapoptions);    google.maps.event.adddomlistener(document.getelementsbyclassname("map-link")[0], 'click', function(e) {      map.setcenter(new google.maps.latlng(42, -72));    });    google.maps.event.adddomlistener(document.getelementsbyclassname("map-link")[1], 'click', function(e) {      map.setcenter(new google.maps.latlng(41, -72.1));    });  }    google.maps.event.adddomlistener(window, 'load', initialize);
html,  body,  #map-canvas {    height: 100%;    width: 100%;    margin: 0px;    padding: 0px  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://maps.googleapis.com/maps/api/js"></script>  <ul>    <li><a class="map-link" href="javascript:void(0)"> show in google maps </a>    </li>    <li><a class="map-link" href="javascript:void(0)"> show place in google maps </a>    </li>  </ul>  <div id="map-canvas" style="border: 2px solid #3872ac;"></div>


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -