Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:25:56+00:00 2026-06-15T03:25:56+00:00

I’m writing some Google Maps API v3 code, which seems to work just fine

  • 0

I’m writing some Google Maps API v3 code, which seems to work just fine with multiple markers, but when there’s only 1, it always plots the marker in the top left of the map, just beyond the visible area:

Screenshot of how the map looks

Here’s my coffeescript code:

class SimpleMap
    constructor: (div_id, lat = 40.783627, lng = -73.942583) ->
        # L.Icon.Default.imagePath = "/assets"
        @div_id = div_id
        @map_options = {center: new google.maps.LatLng(lat, lng), zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP}
        @markers = []
        @map = new google.maps.Map document.getElementById(div_id), @map_options
        @loadMarkers() # gets them and plots on the map
        @autoFit()
    loadMarkers: ->
        items = $(".grid-item[data-lat], .apartment[data-lat]")
        for item in items
        console.log "Adding #{item}"
        @addMarker(item)
    @autoFit()

    addMarker: (item) ->
        console.log "Adding marker"
        lat = $(item).attr("data-lat")
        lng = $(item).attr("data-lng")

        console.log "#{lat}, #{lng}"

        marker = new google.maps.Marker(
            position: new google.maps.LatLng lat, lng
            map: @map
            title: "This is my marker"
        )

        @markers.push marker

    autoFit: ->
        bounds = new google.maps.LatLngBounds()
        for marker in @markers
            bounds.extend marker.getPosition()
        @map.fitBounds bounds
        # if you leave out the below, the marker appears int he same position as in the screenshot (slightly off screen) but at the max zoom level. 
        listener = google.maps.event.addListener(@map, "idle", =>
            @map.setZoom 9 if @map.getZoom() > 8
            @map.setCenter @markers[0].getPosition()
            google.maps.event.removeListener listener
        )

The map seems to ignore my attempts to set setCenter(@markers[0].getPosition()). Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:25:57+00:00Added an answer on June 15, 2026 at 3:25 am

    I believe the issue is in:

        bounds = new google.maps.LatLngBounds()
        for marker in @markers
            bounds.extend marker.getPosition()
        @map.fitBounds bounds
    

    where you are extending the current map bounds to include all markers, but you have only one marker, the bounds will extend in a way that the marker will be in the map limit border.

    Regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.