中心地图和设置默认位置

时间:2017-01-27 04:23:21

标签: javascript google-maps google-maps-api-3

我想将谷歌地图标记居中,我还希望它在没有发生点击事件时显示默认位置(本例中是班加罗尔)。我在哪里可以改变它?

[Sales] + ([Sales] / Sum(CASE WHEN [Customer] <> 'Not assigned' THEN [Sales] ELSE 0 END) OVER( partition BY [Year], Market) 
                   * Sum(CASE WHEN [Customer] = 'Not assigned' THEN [Sales] ELSE 0 END) OVER( partition BY [Year], Market)) AS result

链接到小提琴:https://jsfiddle.net/gLcac3qa/

2 个答案:

答案 0 :(得分:2)

  1. 使“BANGALOR”成为默认值(我假设您要打开infowindow),在创建标记后触发click事件。
  2. //Set default to Bangalore
    google.maps.event.trigger(gmarkers['BANGALORE'],"click");
    
    1. 如果要在HTML中单击名称时使标记居中,请编写代码(在标记单击侦听器中):
    2. google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(html);
          infowindow.open(map, marker);
          map.setCenter(this.getPosition())
      });
      

      proof of concept fiddle

      代码段

      var locations = [
        [
          "BANGALORE",
          "215 West Girard Avenue 19123",
          "12.9716",
          "77.5946"
        ],
        [
          "JAIPUR",
          "5360 Old York Road 19141",
          "26.9124",
          "75.7873"
        ],
        [
          "MUMBAI",
          "1350 W Girard Avenue 19123",
          "19.0760",
          "72.8777"
        ],
        [
          "HYDERABAD",
          "1950 W Girard Avenue 19123",
          "17.3850",
          "78.4867"
        ],
        [
          "CHENNAI",
          "1950 W Girard Avenue 19123",
          "13.0827",
          "78.4867"
        ]
      ];
      
      gmarkers = [];
      
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 12,
        center: new google.maps.LatLng(12.9716, 77.5946),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      
      var infowindow = new google.maps.InfoWindow();
      
      
      function createMarker(latlng, html) {
        var marker = new google.maps.Marker({
          position: latlng,
          map: map
        });
      
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(html);
          infowindow.open(map, marker);
          map.setCenter(this.getPosition())
        });
        return marker;
      }
      
      for (var i = 0; i < locations.length; i++) {
        gmarkers[locations[i][0]] =
          createMarker(new google.maps.LatLng(locations[i][2], locations[i][3]), locations[i][0] + "<br>" + locations[i][1]);
      }
      
      //Set default to Bangalore
      google.maps.event.trigger(gmarkers['BANGALORE'], "click");
      #section23 {
        background: white;
        padding: 80px;
        text-align: center;
      }
      #div23 {
        padding-right: 20px;
        padding-left: 20px;
      }
      #section23 a {
        text-align: center;
        font-size: 20px;
        font-weight: bold;
        font-family: "ProximaNova-Bold";
        text-decoration: none;
        color: black;
        padding: 30px;
      }
      .clickableDiv {
        border: black 1px solid;
      }
      #map {
        width: 100%;
        height: 450px;
        position: relative;
      }
      <script src="https://maps.googleapis.com/maps/api/js"></script>
      <section id="section23">
        <div id="div23">
          <a id="loc1" class="clickableDiv" href="javascript:google.maps.event.trigger(gmarkers['BANGALORE'],'click');">BANGALORE</a>
          <a id="loc2" class="clickableDiv" href="javascript:google.maps.event.trigger(gmarkers['JAIPUR'],'click');">JAIPUR</a>
          <a id="loc3" class="clickableDiv" href="javascript:google.maps.event.trigger(gmarkers['MUMBAI'],'click');">MUMBAI</a>
          <a id="loc4" class="clickableDiv" href="javascript:google.maps.event.trigger(gmarkers['HYDERABAD'],'click');">HYDERABAD</a>
          <a id="loc5" class="clickableDiv" href="javascript:google.maps.event.trigger(gmarkers['CHENNAI'],'click');">CHENNAI</a>
        </div>
      </section>
      
      <section id="map"></section>

答案 1 :(得分:0)

// Marker code starts
var infowindow = new google.maps.InfoWindow();
var mylatlng = new google.maps.LatLng(39.9995601,-75.1395161);
var marker = new google.maps.Marker({
    position: mylatlng,
    map: map
});
//Create marker function goes here