0

How to left align the google maps, I mean the marker positions. Here is my JS

function showGoogleMaps() {

    var latLng = new google.maps.LatLng(position[0], position[1]);

    var mapOptions = {
        zoom: 16, 
        zoomControl:false,
        mapTypeControl:false,
        streetViewControl: false, // hide the yellow Street View pegman
        scaleControl: true, // allow users to zoom the Google Map
        mapTypeId:ROADMAP,
        center: latLng,
        scrollwheel: false,
        styles: styles

    };

    map = new google.maps.Map(document.getElementById('googlemaps'),
        mapOptions);

    // Show the default red marker at the location
    marker = new google.maps.Marker({
        position: latLng,
        map: map,
        draggable: false,
        title: 'Hello',
        animation: google.maps.Animation.DROP
    });
}

Here are the screenshots

The default marker location The default marker location

How I want this to be How I want this to be

The marker or location is centered by default. I want that either to the left or right by default.

Thanks

duncan
  • 31,401
  • 13
  • 78
  • 99
Ayanize
  • 485
  • 1
  • 4
  • 21
  • Change the position of the marker by subtracting an amount from the longitude (position[1]). – sideroxylon Feb 21 '16 at 08:25
  • Thanks I tried but the marker is still centered positioned. – Ayanize Feb 21 '16 at 08:37
  • Please show your code to offset the marker You need something like `var offset = position[1] - 0.5; var marker_position = new google.maps.LatLng(position[0], offset);` and then set the marker psotion to `marker_position`. – sideroxylon Feb 21 '16 at 08:49
  • possible duplicate of [How to offset the center point in Google maps api V3](http://stackoverflow.com/questions/10656743/how-to-offset-the-center-point-in-google-maps-api-v3) – geocodezip Feb 21 '16 at 17:27
  • possible duplicate of [How to offset the center of a Google maps (API v3) in pixels?](http://stackoverflow.com/questions/3473367/how-to-offset-the-center-of-a-google-maps-api-v3-in-pixels) – geocodezip Feb 21 '16 at 17:27

1 Answers1

1

A simple approach:

Initially set the width of the map to e.g 30%.

The center of the map now will be in the middle of these 30%.

Once the map is loaded set the width of the map to 100%.

function initialize() {

  var goo = google.maps,
    map = new goo.Map(document.getElementById('googleMap'), {}),
    markers = [{
      pos: new goo.LatLng(26.1445169, 91.7362)
    }, {
      pos: new goo.LatLng(26.2571285, 92.05991)
    }, {
      pos: new goo.LatLng(26.3143518, 91.0497609)
    }, ],
    bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < markers.length; ++i) {
    new google.maps.Marker({
      map: map,
      position: markers[i].pos
    });
    bounds.extend(markers[i].pos);
  }
  map.fitBounds(bounds);

  goo.event.addListenerOnce(map, 'idle', function() {

    this.getDiv().style.width = '100%';
    goo.event.trigger(this, 'resize');
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_wrapper,
#googleMap {
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
#googleMap {
  width: 30%;
}
<div id="map_wrapper">
  <div id="googleMap"></div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>

To align the markers on the right additionally use the panBy-method:

function initialize() {

  var goo = google.maps,
    map = new goo.Map(document.getElementById('googleMap'), {}),
    markers = [{
      pos: new goo.LatLng(26.1445169, 91.7362)
    }, {
      pos: new goo.LatLng(26.2571285, 92.05991)
    }, {
      pos: new goo.LatLng(26.3143518, 91.0497609)
    }, ],
    bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < markers.length; ++i) {
    new google.maps.Marker({
      map: map,
      position: markers[i].pos
    });
    bounds.extend(markers[i].pos);
  }
  map.fitBounds(bounds);

  goo.event.addListenerOnce(map, 'idle', function() {

    this.getDiv().style.width = '100%';
    this.panBy(-(this.getDiv().offsetWidth/1.5),0);
    goo.event.trigger(this, 'resize');
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_wrapper,
#googleMap {
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
#googleMap {
  width: 30%;
}
<div id="map_wrapper">
  <div id="googleMap"></div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201