Showing a point on GoogleMaps after clicking a button









up vote
0
down vote

favorite












I have Google Maps with many markers.
This is my code:



<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
<a href ="#" class="obj-1" id="obj-1"><div class="apartament_atrakcje">Atrakcja 1 pl</div></a>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
<a href ="#" class="obj-2" id="obj-2"><div class="apartament_atrakcje">Atrakcja 2 PL</div></a>
</div>




<div id="map_canvas"></div>

<script>
window.onload = function ()
var styles = ["featureType":"all"];
var bounds = new google.maps.LatLngBounds();
var options =
mapTypeControlOptions:
mapTypeIds: ['Styled']
,
center: new google.maps.LatLng(11, 22),
zoom: 15,
disableDefaultUI: true,
mapTypeId: 'Styled'
;
marker = new google.maps.Marker(
map:map,
);
var div = document.getElementById('map_canvas');
var map = new google.maps.Map(div, options);
var styledMapType = new google.maps.StyledMapType(styles, name: 'Styled' );


var markers = [
['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
];
var infoWindow= new google.maps.InfoWindow(maxWidth:600),
marker, i,
image = 'http://localhost/apartamenty/assets/poi.png';

for( i = 0; i < markers.length; i++ )
var beach = markers[i];
var position = new google.maps.LatLng(beach[1], beach[2]);
bounds.extend(position);
marker = new google.maps.Marker(
position: position,
map: map,
icon: 'http://localhost/apartamenty/assets/' + beach[5],
title: beach[0],
myurl: beach[4]
);

google.maps.event.addListener(marker, 'click', (function(marker, i)
return function()
infoWindow.setContent('<a href="'+marker['myurl']+'">'+marker['title']+'</a>');
infoWindow.open(map, marker);

)(marker, i));
map.fitBounds(bounds);
map.mapTypes.set('Styled', styledMapType);



</script>


I have a map and the markers on it. This is ok!



After clicking on link obj-1 or obj-2 I would like to:



- center the map on this one, selected marker 
- display the marker of this marker


If the user clicks on the link "Atrakcja 1 pl" - then map is center on marker with title Atrakcja 1 pll and his hint would be visible.



Does anyone know how to do it in my code?










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have Google Maps with many markers.
    This is my code:



    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
    <a href ="#" class="obj-1" id="obj-1"><div class="apartament_atrakcje">Atrakcja 1 pl</div></a>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
    <a href ="#" class="obj-2" id="obj-2"><div class="apartament_atrakcje">Atrakcja 2 PL</div></a>
    </div>




    <div id="map_canvas"></div>

    <script>
    window.onload = function ()
    var styles = ["featureType":"all"];
    var bounds = new google.maps.LatLngBounds();
    var options =
    mapTypeControlOptions:
    mapTypeIds: ['Styled']
    ,
    center: new google.maps.LatLng(11, 22),
    zoom: 15,
    disableDefaultUI: true,
    mapTypeId: 'Styled'
    ;
    marker = new google.maps.Marker(
    map:map,
    );
    var div = document.getElementById('map_canvas');
    var map = new google.maps.Map(div, options);
    var styledMapType = new google.maps.StyledMapType(styles, name: 'Styled' );


    var markers = [
    ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
    ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
    ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
    ];
    var infoWindow= new google.maps.InfoWindow(maxWidth:600),
    marker, i,
    image = 'http://localhost/apartamenty/assets/poi.png';

    for( i = 0; i < markers.length; i++ )
    var beach = markers[i];
    var position = new google.maps.LatLng(beach[1], beach[2]);
    bounds.extend(position);
    marker = new google.maps.Marker(
    position: position,
    map: map,
    icon: 'http://localhost/apartamenty/assets/' + beach[5],
    title: beach[0],
    myurl: beach[4]
    );

    google.maps.event.addListener(marker, 'click', (function(marker, i)
    return function()
    infoWindow.setContent('<a href="'+marker['myurl']+'">'+marker['title']+'</a>');
    infoWindow.open(map, marker);

    )(marker, i));
    map.fitBounds(bounds);
    map.mapTypes.set('Styled', styledMapType);



    </script>


    I have a map and the markers on it. This is ok!



    After clicking on link obj-1 or obj-2 I would like to:



    - center the map on this one, selected marker 
    - display the marker of this marker


    If the user clicks on the link "Atrakcja 1 pl" - then map is center on marker with title Atrakcja 1 pll and his hint would be visible.



    Does anyone know how to do it in my code?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have Google Maps with many markers.
      This is my code:



      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
      <a href ="#" class="obj-1" id="obj-1"><div class="apartament_atrakcje">Atrakcja 1 pl</div></a>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
      <a href ="#" class="obj-2" id="obj-2"><div class="apartament_atrakcje">Atrakcja 2 PL</div></a>
      </div>




      <div id="map_canvas"></div>

      <script>
      window.onload = function ()
      var styles = ["featureType":"all"];
      var bounds = new google.maps.LatLngBounds();
      var options =
      mapTypeControlOptions:
      mapTypeIds: ['Styled']
      ,
      center: new google.maps.LatLng(11, 22),
      zoom: 15,
      disableDefaultUI: true,
      mapTypeId: 'Styled'
      ;
      marker = new google.maps.Marker(
      map:map,
      );
      var div = document.getElementById('map_canvas');
      var map = new google.maps.Map(div, options);
      var styledMapType = new google.maps.StyledMapType(styles, name: 'Styled' );


      var markers = [
      ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
      ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
      ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
      ];
      var infoWindow= new google.maps.InfoWindow(maxWidth:600),
      marker, i,
      image = 'http://localhost/apartamenty/assets/poi.png';

      for( i = 0; i < markers.length; i++ )
      var beach = markers[i];
      var position = new google.maps.LatLng(beach[1], beach[2]);
      bounds.extend(position);
      marker = new google.maps.Marker(
      position: position,
      map: map,
      icon: 'http://localhost/apartamenty/assets/' + beach[5],
      title: beach[0],
      myurl: beach[4]
      );

      google.maps.event.addListener(marker, 'click', (function(marker, i)
      return function()
      infoWindow.setContent('<a href="'+marker['myurl']+'">'+marker['title']+'</a>');
      infoWindow.open(map, marker);

      )(marker, i));
      map.fitBounds(bounds);
      map.mapTypes.set('Styled', styledMapType);



      </script>


      I have a map and the markers on it. This is ok!



      After clicking on link obj-1 or obj-2 I would like to:



      - center the map on this one, selected marker 
      - display the marker of this marker


      If the user clicks on the link "Atrakcja 1 pl" - then map is center on marker with title Atrakcja 1 pll and his hint would be visible.



      Does anyone know how to do it in my code?










      share|improve this question















      I have Google Maps with many markers.
      This is my code:



      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
      <a href ="#" class="obj-1" id="obj-1"><div class="apartament_atrakcje">Atrakcja 1 pl</div></a>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
      <a href ="#" class="obj-2" id="obj-2"><div class="apartament_atrakcje">Atrakcja 2 PL</div></a>
      </div>




      <div id="map_canvas"></div>

      <script>
      window.onload = function ()
      var styles = ["featureType":"all"];
      var bounds = new google.maps.LatLngBounds();
      var options =
      mapTypeControlOptions:
      mapTypeIds: ['Styled']
      ,
      center: new google.maps.LatLng(11, 22),
      zoom: 15,
      disableDefaultUI: true,
      mapTypeId: 'Styled'
      ;
      marker = new google.maps.Marker(
      map:map,
      );
      var div = document.getElementById('map_canvas');
      var map = new google.maps.Map(div, options);
      var styledMapType = new google.maps.StyledMapType(styles, name: 'Styled' );


      var markers = [
      ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
      ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
      ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
      ];
      var infoWindow= new google.maps.InfoWindow(maxWidth:600),
      marker, i,
      image = 'http://localhost/apartamenty/assets/poi.png';

      for( i = 0; i < markers.length; i++ )
      var beach = markers[i];
      var position = new google.maps.LatLng(beach[1], beach[2]);
      bounds.extend(position);
      marker = new google.maps.Marker(
      position: position,
      map: map,
      icon: 'http://localhost/apartamenty/assets/' + beach[5],
      title: beach[0],
      myurl: beach[4]
      );

      google.maps.event.addListener(marker, 'click', (function(marker, i)
      return function()
      infoWindow.setContent('<a href="'+marker['myurl']+'">'+marker['title']+'</a>');
      infoWindow.open(map, marker);

      )(marker, i));
      map.fitBounds(bounds);
      map.mapTypes.set('Styled', styledMapType);



      </script>


      I have a map and the markers on it. This is ok!



      After clicking on link obj-1 or obj-2 I would like to:



      - center the map on this one, selected marker 
      - display the marker of this marker


      If the user clicks on the link "Atrakcja 1 pl" - then map is center on marker with title Atrakcja 1 pll and his hint would be visible.



      Does anyone know how to do it in my code?







      google-maps google-maps-api-3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 16:25









      geocodezip

      124k10140169




      124k10140169










      asked Nov 10 at 13:11









      delifer

      153




      153






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          One option would be to keep references to the markers in an array and add DOM click listeners to your "links" to open the marker's InfoWindow and center the map on that marker.



          1. create an array of markers:

          var gmarkers=;

          for (i = 0; i < markers.length; i++) {
          var beach = markers[i];
          var position = new google.maps.LatLng(beach[1], beach[2]);
          bounds.extend(position);
          marker = new google.maps.Marker(
          position: position,
          map: map,
          // icon: 'http://localhost/apartamenty/assets/' + beach[5],
          title: beach[0],
          myurl: beach[4]
          );
          gmarkers.push(marker);
          // ...


          1. add click listeners to your "links" that opens that marker's InfoWindow and centers the map on its position:

           if (document.getElementById('obj-'+(i+1))) 
          // relies on the naming convention in your posted code
          google.maps.event.addDomListener(document.getElementById('obj-'+(i+1)), 'click', function(i)
          return function()
          google.maps.event.trigger(gmarkers[i], 'click');
          map.setCenter(gmarkers[i].getPosition());
          (i))



          proof of concept fiddle



          code snippet:






          html,
          body,
          #map_canvas
          height: 90%;
          width: 100%;
          padding: 0px;
          margin: 0px;

          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
          <a href="#" class="obj-1" id="obj-1">
          <div class="apartament_atrakcje">Atrakcja 1 pl</div>
          </a>
          </div>
          <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
          <a href="#" class="obj-2" id="obj-2">
          <div class="apartament_atrakcje">Atrakcja 2 PL</div>
          </a>
          </div>
          <div id="map_canvas"></div>
          <script>
          window.onload = function()
          var styles = [
          "featureType": "all"
          ];
          var gmarkers = ;
          var bounds = new google.maps.LatLngBounds();
          var options =
          mapTypeControlOptions:
          mapTypeIds: ['Styled']
          ,
          center: new google.maps.LatLng(11, 22),
          zoom: 15,
          disableDefaultUI: true,
          mapTypeId: 'Styled'
          ;
          marker = new google.maps.Marker(
          map: map,
          );
          var div = document.getElementById('map_canvas');
          var map = new google.maps.Map(div, options);
          var styledMapType = new google.maps.StyledMapType(styles,
          name: 'Styled'
          );

          var markers = [
          ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
          ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
          ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
          ];
          var infoWindow = new google.maps.InfoWindow(
          maxWidth: 600
          ),
          marker, i,
          image = 'http://localhost/apartamenty/assets/poi.png';

          for (i = 0; i < markers.length; i++)
          var beach = markers[i];
          var position = new google.maps.LatLng(beach[1], beach[2]);
          bounds.extend(position);
          marker = new google.maps.Marker(
          position: position,
          map: map,
          // icon: 'http://localhost/apartamenty/assets/' + beach[5],
          title: beach[0],
          myurl: beach[4]
          );
          gmarkers.push(marker);

          google.maps.event.addListener(marker, 'click', (function(marker, i)
          return function()
          infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
          infoWindow.open(map, marker);

          )(marker, i));
          map.fitBounds(bounds);
          map.mapTypes.set('Styled', styledMapType);
          if (document.getElementById('obj-' + (i + 1)))
          // relies on the naming convention in your posted code
          google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
          return function()
          google.maps.event.trigger(gmarkers[i], 'click');
          map.setCenter(gmarkers[i].getPosition());

          (i))



          </script>








          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239276%2fshowing-a-point-on-googlemaps-after-clicking-a-button%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            One option would be to keep references to the markers in an array and add DOM click listeners to your "links" to open the marker's InfoWindow and center the map on that marker.



            1. create an array of markers:

            var gmarkers=;

            for (i = 0; i < markers.length; i++) {
            var beach = markers[i];
            var position = new google.maps.LatLng(beach[1], beach[2]);
            bounds.extend(position);
            marker = new google.maps.Marker(
            position: position,
            map: map,
            // icon: 'http://localhost/apartamenty/assets/' + beach[5],
            title: beach[0],
            myurl: beach[4]
            );
            gmarkers.push(marker);
            // ...


            1. add click listeners to your "links" that opens that marker's InfoWindow and centers the map on its position:

             if (document.getElementById('obj-'+(i+1))) 
            // relies on the naming convention in your posted code
            google.maps.event.addDomListener(document.getElementById('obj-'+(i+1)), 'click', function(i)
            return function()
            google.maps.event.trigger(gmarkers[i], 'click');
            map.setCenter(gmarkers[i].getPosition());
            (i))



            proof of concept fiddle



            code snippet:






            html,
            body,
            #map_canvas
            height: 90%;
            width: 100%;
            padding: 0px;
            margin: 0px;

            <script src="https://maps.googleapis.com/maps/api/js"></script>
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
            <a href="#" class="obj-1" id="obj-1">
            <div class="apartament_atrakcje">Atrakcja 1 pl</div>
            </a>
            </div>
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
            <a href="#" class="obj-2" id="obj-2">
            <div class="apartament_atrakcje">Atrakcja 2 PL</div>
            </a>
            </div>
            <div id="map_canvas"></div>
            <script>
            window.onload = function()
            var styles = [
            "featureType": "all"
            ];
            var gmarkers = ;
            var bounds = new google.maps.LatLngBounds();
            var options =
            mapTypeControlOptions:
            mapTypeIds: ['Styled']
            ,
            center: new google.maps.LatLng(11, 22),
            zoom: 15,
            disableDefaultUI: true,
            mapTypeId: 'Styled'
            ;
            marker = new google.maps.Marker(
            map: map,
            );
            var div = document.getElementById('map_canvas');
            var map = new google.maps.Map(div, options);
            var styledMapType = new google.maps.StyledMapType(styles,
            name: 'Styled'
            );

            var markers = [
            ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
            ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
            ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
            ];
            var infoWindow = new google.maps.InfoWindow(
            maxWidth: 600
            ),
            marker, i,
            image = 'http://localhost/apartamenty/assets/poi.png';

            for (i = 0; i < markers.length; i++)
            var beach = markers[i];
            var position = new google.maps.LatLng(beach[1], beach[2]);
            bounds.extend(position);
            marker = new google.maps.Marker(
            position: position,
            map: map,
            // icon: 'http://localhost/apartamenty/assets/' + beach[5],
            title: beach[0],
            myurl: beach[4]
            );
            gmarkers.push(marker);

            google.maps.event.addListener(marker, 'click', (function(marker, i)
            return function()
            infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
            infoWindow.open(map, marker);

            )(marker, i));
            map.fitBounds(bounds);
            map.mapTypes.set('Styled', styledMapType);
            if (document.getElementById('obj-' + (i + 1)))
            // relies on the naming convention in your posted code
            google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
            return function()
            google.maps.event.trigger(gmarkers[i], 'click');
            map.setCenter(gmarkers[i].getPosition());

            (i))



            </script>








            share|improve this answer
























              up vote
              0
              down vote



              accepted










              One option would be to keep references to the markers in an array and add DOM click listeners to your "links" to open the marker's InfoWindow and center the map on that marker.



              1. create an array of markers:

              var gmarkers=;

              for (i = 0; i < markers.length; i++) {
              var beach = markers[i];
              var position = new google.maps.LatLng(beach[1], beach[2]);
              bounds.extend(position);
              marker = new google.maps.Marker(
              position: position,
              map: map,
              // icon: 'http://localhost/apartamenty/assets/' + beach[5],
              title: beach[0],
              myurl: beach[4]
              );
              gmarkers.push(marker);
              // ...


              1. add click listeners to your "links" that opens that marker's InfoWindow and centers the map on its position:

               if (document.getElementById('obj-'+(i+1))) 
              // relies on the naming convention in your posted code
              google.maps.event.addDomListener(document.getElementById('obj-'+(i+1)), 'click', function(i)
              return function()
              google.maps.event.trigger(gmarkers[i], 'click');
              map.setCenter(gmarkers[i].getPosition());
              (i))



              proof of concept fiddle



              code snippet:






              html,
              body,
              #map_canvas
              height: 90%;
              width: 100%;
              padding: 0px;
              margin: 0px;

              <script src="https://maps.googleapis.com/maps/api/js"></script>
              <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
              <a href="#" class="obj-1" id="obj-1">
              <div class="apartament_atrakcje">Atrakcja 1 pl</div>
              </a>
              </div>
              <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
              <a href="#" class="obj-2" id="obj-2">
              <div class="apartament_atrakcje">Atrakcja 2 PL</div>
              </a>
              </div>
              <div id="map_canvas"></div>
              <script>
              window.onload = function()
              var styles = [
              "featureType": "all"
              ];
              var gmarkers = ;
              var bounds = new google.maps.LatLngBounds();
              var options =
              mapTypeControlOptions:
              mapTypeIds: ['Styled']
              ,
              center: new google.maps.LatLng(11, 22),
              zoom: 15,
              disableDefaultUI: true,
              mapTypeId: 'Styled'
              ;
              marker = new google.maps.Marker(
              map: map,
              );
              var div = document.getElementById('map_canvas');
              var map = new google.maps.Map(div, options);
              var styledMapType = new google.maps.StyledMapType(styles,
              name: 'Styled'
              );

              var markers = [
              ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
              ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
              ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
              ];
              var infoWindow = new google.maps.InfoWindow(
              maxWidth: 600
              ),
              marker, i,
              image = 'http://localhost/apartamenty/assets/poi.png';

              for (i = 0; i < markers.length; i++)
              var beach = markers[i];
              var position = new google.maps.LatLng(beach[1], beach[2]);
              bounds.extend(position);
              marker = new google.maps.Marker(
              position: position,
              map: map,
              // icon: 'http://localhost/apartamenty/assets/' + beach[5],
              title: beach[0],
              myurl: beach[4]
              );
              gmarkers.push(marker);

              google.maps.event.addListener(marker, 'click', (function(marker, i)
              return function()
              infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
              infoWindow.open(map, marker);

              )(marker, i));
              map.fitBounds(bounds);
              map.mapTypes.set('Styled', styledMapType);
              if (document.getElementById('obj-' + (i + 1)))
              // relies on the naming convention in your posted code
              google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
              return function()
              google.maps.event.trigger(gmarkers[i], 'click');
              map.setCenter(gmarkers[i].getPosition());

              (i))



              </script>








              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                One option would be to keep references to the markers in an array and add DOM click listeners to your "links" to open the marker's InfoWindow and center the map on that marker.



                1. create an array of markers:

                var gmarkers=;

                for (i = 0; i < markers.length; i++) {
                var beach = markers[i];
                var position = new google.maps.LatLng(beach[1], beach[2]);
                bounds.extend(position);
                marker = new google.maps.Marker(
                position: position,
                map: map,
                // icon: 'http://localhost/apartamenty/assets/' + beach[5],
                title: beach[0],
                myurl: beach[4]
                );
                gmarkers.push(marker);
                // ...


                1. add click listeners to your "links" that opens that marker's InfoWindow and centers the map on its position:

                 if (document.getElementById('obj-'+(i+1))) 
                // relies on the naming convention in your posted code
                google.maps.event.addDomListener(document.getElementById('obj-'+(i+1)), 'click', function(i)
                return function()
                google.maps.event.trigger(gmarkers[i], 'click');
                map.setCenter(gmarkers[i].getPosition());
                (i))



                proof of concept fiddle



                code snippet:






                html,
                body,
                #map_canvas
                height: 90%;
                width: 100%;
                padding: 0px;
                margin: 0px;

                <script src="https://maps.googleapis.com/maps/api/js"></script>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-1" id="obj-1">
                <div class="apartament_atrakcje">Atrakcja 1 pl</div>
                </a>
                </div>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-2" id="obj-2">
                <div class="apartament_atrakcje">Atrakcja 2 PL</div>
                </a>
                </div>
                <div id="map_canvas"></div>
                <script>
                window.onload = function()
                var styles = [
                "featureType": "all"
                ];
                var gmarkers = ;
                var bounds = new google.maps.LatLngBounds();
                var options =
                mapTypeControlOptions:
                mapTypeIds: ['Styled']
                ,
                center: new google.maps.LatLng(11, 22),
                zoom: 15,
                disableDefaultUI: true,
                mapTypeId: 'Styled'
                ;
                marker = new google.maps.Marker(
                map: map,
                );
                var div = document.getElementById('map_canvas');
                var map = new google.maps.Map(div, options);
                var styledMapType = new google.maps.StyledMapType(styles,
                name: 'Styled'
                );

                var markers = [
                ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
                ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
                ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
                ];
                var infoWindow = new google.maps.InfoWindow(
                maxWidth: 600
                ),
                marker, i,
                image = 'http://localhost/apartamenty/assets/poi.png';

                for (i = 0; i < markers.length; i++)
                var beach = markers[i];
                var position = new google.maps.LatLng(beach[1], beach[2]);
                bounds.extend(position);
                marker = new google.maps.Marker(
                position: position,
                map: map,
                // icon: 'http://localhost/apartamenty/assets/' + beach[5],
                title: beach[0],
                myurl: beach[4]
                );
                gmarkers.push(marker);

                google.maps.event.addListener(marker, 'click', (function(marker, i)
                return function()
                infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
                infoWindow.open(map, marker);

                )(marker, i));
                map.fitBounds(bounds);
                map.mapTypes.set('Styled', styledMapType);
                if (document.getElementById('obj-' + (i + 1)))
                // relies on the naming convention in your posted code
                google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
                return function()
                google.maps.event.trigger(gmarkers[i], 'click');
                map.setCenter(gmarkers[i].getPosition());

                (i))



                </script>








                share|improve this answer












                One option would be to keep references to the markers in an array and add DOM click listeners to your "links" to open the marker's InfoWindow and center the map on that marker.



                1. create an array of markers:

                var gmarkers=;

                for (i = 0; i < markers.length; i++) {
                var beach = markers[i];
                var position = new google.maps.LatLng(beach[1], beach[2]);
                bounds.extend(position);
                marker = new google.maps.Marker(
                position: position,
                map: map,
                // icon: 'http://localhost/apartamenty/assets/' + beach[5],
                title: beach[0],
                myurl: beach[4]
                );
                gmarkers.push(marker);
                // ...


                1. add click listeners to your "links" that opens that marker's InfoWindow and centers the map on its position:

                 if (document.getElementById('obj-'+(i+1))) 
                // relies on the naming convention in your posted code
                google.maps.event.addDomListener(document.getElementById('obj-'+(i+1)), 'click', function(i)
                return function()
                google.maps.event.trigger(gmarkers[i], 'click');
                map.setCenter(gmarkers[i].getPosition());
                (i))



                proof of concept fiddle



                code snippet:






                html,
                body,
                #map_canvas
                height: 90%;
                width: 100%;
                padding: 0px;
                margin: 0px;

                <script src="https://maps.googleapis.com/maps/api/js"></script>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-1" id="obj-1">
                <div class="apartament_atrakcje">Atrakcja 1 pl</div>
                </a>
                </div>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-2" id="obj-2">
                <div class="apartament_atrakcje">Atrakcja 2 PL</div>
                </a>
                </div>
                <div id="map_canvas"></div>
                <script>
                window.onload = function()
                var styles = [
                "featureType": "all"
                ];
                var gmarkers = ;
                var bounds = new google.maps.LatLngBounds();
                var options =
                mapTypeControlOptions:
                mapTypeIds: ['Styled']
                ,
                center: new google.maps.LatLng(11, 22),
                zoom: 15,
                disableDefaultUI: true,
                mapTypeId: 'Styled'
                ;
                marker = new google.maps.Marker(
                map: map,
                );
                var div = document.getElementById('map_canvas');
                var map = new google.maps.Map(div, options);
                var styledMapType = new google.maps.StyledMapType(styles,
                name: 'Styled'
                );

                var markers = [
                ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
                ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
                ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
                ];
                var infoWindow = new google.maps.InfoWindow(
                maxWidth: 600
                ),
                marker, i,
                image = 'http://localhost/apartamenty/assets/poi.png';

                for (i = 0; i < markers.length; i++)
                var beach = markers[i];
                var position = new google.maps.LatLng(beach[1], beach[2]);
                bounds.extend(position);
                marker = new google.maps.Marker(
                position: position,
                map: map,
                // icon: 'http://localhost/apartamenty/assets/' + beach[5],
                title: beach[0],
                myurl: beach[4]
                );
                gmarkers.push(marker);

                google.maps.event.addListener(marker, 'click', (function(marker, i)
                return function()
                infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
                infoWindow.open(map, marker);

                )(marker, i));
                map.fitBounds(bounds);
                map.mapTypes.set('Styled', styledMapType);
                if (document.getElementById('obj-' + (i + 1)))
                // relies on the naming convention in your posted code
                google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
                return function()
                google.maps.event.trigger(gmarkers[i], 'click');
                map.setCenter(gmarkers[i].getPosition());

                (i))



                </script>








                html,
                body,
                #map_canvas
                height: 90%;
                width: 100%;
                padding: 0px;
                margin: 0px;

                <script src="https://maps.googleapis.com/maps/api/js"></script>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-1" id="obj-1">
                <div class="apartament_atrakcje">Atrakcja 1 pl</div>
                </a>
                </div>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-2" id="obj-2">
                <div class="apartament_atrakcje">Atrakcja 2 PL</div>
                </a>
                </div>
                <div id="map_canvas"></div>
                <script>
                window.onload = function()
                var styles = [
                "featureType": "all"
                ];
                var gmarkers = ;
                var bounds = new google.maps.LatLngBounds();
                var options =
                mapTypeControlOptions:
                mapTypeIds: ['Styled']
                ,
                center: new google.maps.LatLng(11, 22),
                zoom: 15,
                disableDefaultUI: true,
                mapTypeId: 'Styled'
                ;
                marker = new google.maps.Marker(
                map: map,
                );
                var div = document.getElementById('map_canvas');
                var map = new google.maps.Map(div, options);
                var styledMapType = new google.maps.StyledMapType(styles,
                name: 'Styled'
                );

                var markers = [
                ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
                ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
                ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
                ];
                var infoWindow = new google.maps.InfoWindow(
                maxWidth: 600
                ),
                marker, i,
                image = 'http://localhost/apartamenty/assets/poi.png';

                for (i = 0; i < markers.length; i++)
                var beach = markers[i];
                var position = new google.maps.LatLng(beach[1], beach[2]);
                bounds.extend(position);
                marker = new google.maps.Marker(
                position: position,
                map: map,
                // icon: 'http://localhost/apartamenty/assets/' + beach[5],
                title: beach[0],
                myurl: beach[4]
                );
                gmarkers.push(marker);

                google.maps.event.addListener(marker, 'click', (function(marker, i)
                return function()
                infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
                infoWindow.open(map, marker);

                )(marker, i));
                map.fitBounds(bounds);
                map.mapTypes.set('Styled', styledMapType);
                if (document.getElementById('obj-' + (i + 1)))
                // relies on the naming convention in your posted code
                google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
                return function()
                google.maps.event.trigger(gmarkers[i], 'click');
                map.setCenter(gmarkers[i].getPosition());

                (i))



                </script>





                html,
                body,
                #map_canvas
                height: 90%;
                width: 100%;
                padding: 0px;
                margin: 0px;

                <script src="https://maps.googleapis.com/maps/api/js"></script>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-1" id="obj-1">
                <div class="apartament_atrakcje">Atrakcja 1 pl</div>
                </a>
                </div>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 padding_all_2">
                <a href="#" class="obj-2" id="obj-2">
                <div class="apartament_atrakcje">Atrakcja 2 PL</div>
                </a>
                </div>
                <div id="map_canvas"></div>
                <script>
                window.onload = function()
                var styles = [
                "featureType": "all"
                ];
                var gmarkers = ;
                var bounds = new google.maps.LatLngBounds();
                var options =
                mapTypeControlOptions:
                mapTypeIds: ['Styled']
                ,
                center: new google.maps.LatLng(11, 22),
                zoom: 15,
                disableDefaultUI: true,
                mapTypeId: 'Styled'
                ;
                marker = new google.maps.Marker(
                map: map,
                );
                var div = document.getElementById('map_canvas');
                var map = new google.maps.Map(div, options);
                var styledMapType = new google.maps.StyledMapType(styles,
                name: 'Styled'
                );

                var markers = [
                ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
                ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
                ['Biskupia', 51.93780943, 15.52505514, 'Biskupia', '#', 'poi2.png']
                ];
                var infoWindow = new google.maps.InfoWindow(
                maxWidth: 600
                ),
                marker, i,
                image = 'http://localhost/apartamenty/assets/poi.png';

                for (i = 0; i < markers.length; i++)
                var beach = markers[i];
                var position = new google.maps.LatLng(beach[1], beach[2]);
                bounds.extend(position);
                marker = new google.maps.Marker(
                position: position,
                map: map,
                // icon: 'http://localhost/apartamenty/assets/' + beach[5],
                title: beach[0],
                myurl: beach[4]
                );
                gmarkers.push(marker);

                google.maps.event.addListener(marker, 'click', (function(marker, i)
                return function()
                infoWindow.setContent('<a href="' + marker['myurl'] + '">' + marker['title'] + '</a>');
                infoWindow.open(map, marker);

                )(marker, i));
                map.fitBounds(bounds);
                map.mapTypes.set('Styled', styledMapType);
                if (document.getElementById('obj-' + (i + 1)))
                // relies on the naming convention in your posted code
                google.maps.event.addDomListener(document.getElementById('obj-' + (i + 1)), 'click', function(i)
                return function()
                google.maps.event.trigger(gmarkers[i], 'click');
                map.setCenter(gmarkers[i].getPosition());

                (i))



                </script>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 16:24









                geocodezip

                124k10140169




                124k10140169



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239276%2fshowing-a-point-on-googlemaps-after-clicking-a-button%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo