Repetitive horizontal Google Maps marker on ImageMapType

<!DOCTYPE html>
<html>
  <head>
    <title>Custom Image Mapping</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
var customTypeOptions = {
  getTileUrl: function(coord, zoom) {
      var bound = Math.pow(2, zoom);
      return 'full-out' +
          '/' + zoom + '/' + coord.x + '/' +
          (bound - coord.y - 1) + '.png';
  },
  tileSize: new google.maps.Size(256, 256),
  maxZoom: 6,
  minZoom: 1,
  radius: 1738000,
  name: 'Custom'
};

var customMapType = new google.maps.ImageMapType(customTypeOptions);


function initialize() {
  var myLatlng = new google.maps.LatLng(0, 0);
  var mapOptions = {
    center: myLatlng,
    zoom: 1,
    streetViewControl: false,
    mapTypeControlOptions: {
      mapTypeIds: ['custom']
    }
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
  map.mapTypes.set('custom', customMapType);
  map.setMapTypeId('custom');

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Hello World!'
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

      </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

The provided code showcases a custom image mapping feature using Google Maps API. The issue encountered involves the repetitive display of markers at lower zoom levels horizontally. Seek advice on avoiding this repetition and consider utilizing Google Maps for its reliability and popularity over Leaflet.js due to its stability.

The setup is based on Ubuntu 14.04 OS.

Answer №1

change the setting of the markers' optimized option to false

Answer №2

If you are still facing this issue, take a look at my approach.

1- Adjust the map zoom level to (2) and add marker positions (latitude, longitude) as shown below:

var minZoomLevel = 2;
map.setZoom(minZoomLevel);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < result.length; i++){
    var latlng = new google.maps.LatLng(result[i].Lat, result[i].Lng);
    bounds.extend(latlng);
});

2- Add an event listener for zoom changes:

google.maps.event.addListener(map, 'zoom_changed', function() {
    if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});

3- Implement a center changed listener (This is where the magic happens):

google.maps.event.addListener(map, 'center_changed', function() 
{ 
   checkBounds(bounds);
}

function checkBounds(allowedBounds) {
    if(allowedBounds.contains(map.getCenter())) {
        return;
    }
    var mapCenter = map.getCenter();
    var X = mapCenter.lng();
    var Y = mapCenter.lat();

    var AmaxX = allowedBounds.getNorthEast().lng();
    var AmaxY = allowedBounds.getNorthEast().lat();
    var AminX = allowedBounds.getSouthWest().lng();
    var AminY = allowedBounds.getSouthWest().lat();

    if (X < AminX) {X = AminX;}
    if (X > AmaxX) {X = AmaxX;}
    if (Y < AminY) {Y = AminY;}
    if (Y > AmaxY) {Y = AmaxY;}

    map.setCenter(new google.maps.LatLng(Y,X));
}

Each time the center changes, it will verify the points and limit the map to a specific area. Adjusting the zoom will display only one world tile, while checking the bounds will limit horizontal scrolling, ensuring your markers appear just once on the map. Set the zoom according to your requirements for optimal results!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to fetch a single document from a nested array using Mongoose

Currently, I am working on a MongoDB database using Mongoose in Node.js. The collection structure resembles the following: { cinema: 'xxx', account: 'yyy', media: { data: [{ id: 1, name: 'zzz& ...

Tips on handling a Vue computed property

I am struggling to dispatch an object that is created within a computed property in vue.js. Being fairly new to this framework, I can't seem to make it work. My goal is to dispatch the object named "updateObject" to the vuex-store. I have tried using ...

Bootstrap justify-content-around is not evenly distributing items

I am attempting to evenly distribute three rows of four bootstrap cards across a jumbotron with space-around, but it is not displaying correctly. I suspect there may be a margin interference issue that is included with Bootstrap. Here is the code I am usin ...

Differences in CSS between IE10 and Chrome are causing compatibility issues

Website in question: What is causing the difference in display between IE 10 and other browsers? The website appears to function correctly on Chrome: IE 10: ...

Step-by-step guide on printing barcode labels from a browser in ReactJS with the Wincode C342C printer

Has anyone here successfully printed from the client side using JavaScript/ReactJS to a Wincode c342c printer? I've installed the qz.io library to allow my JavaScript code to access the client's printer. I've managed to print a PDF as base6 ...

Monitoring the validity of form inputs using AngularJS's $watch functionality

Why isn't this deep watch triggering as expected? http://plnkr.co/edit/6FxMS4RlfljBMkprZYQs?p=preview Could it be that the watch function is not checking Angular attributes starting with $? If so, is there a way to monitor changes in validity beyond ...

How can the checkers code be corrected due to a mistake?

Designed a simple game where the objective is to clear all the pieces by jumping over the checkers. However, encountering an error when attempting to remove the checker for the second time. Uncaught TypeError: Cannot read property 'theRow' of u ...

Implementing Endless Scrolling on Quora with Selenium using a Combination of Python and JavaScript

Managing the challenge of "infinite scrolling" on the Quora website has been quite a task for me. After attempting to utilize the send_keys method with Selenium library in Python, I resorted to running JavaScript commands to scroll down the page. Unfortun ...

Automatically downloading files when displayed or viewed on a website

Is it possible for PDF or png/jpeg files viewed on a website to be automatically downloaded to my computer? If so, where would they be stored? I have tried searching on Google but have not found a helpful answer to my question. ...

typescript error caused by NaN

Apologies for the repetitive question, but I am really struggling to find a solution. I am facing an issue with this calculation. The parameters a to g represent the values of my input from the HTML. I need to use these values to calculate a sum. When I tr ...

How to resolve useState problem in useEffect when state is not null

Encountering issues with maintaining state in TypeScript React. A Child component passes a 'terminal' object to the Parent via a function called returnTerminal(). This 'terminal' object is then stored as a useState variable named _obje ...

Checkbox fails to display as checked

I am currently working on implementing checkbox inputs in React JS. I have encountered an issue where the checkboxes do not visually show as checked when clicked, but I am still able to retrieve the value of the checkbox. Here is my code: Simple Checkbox I ...

Adding multiple text as label and sublabel in a React MUI Tooltip can be achieved by utilizing the appropriate

I'm struggling to incorporate a MaterialUI Tooltip into my React application with two separate text lines. The first line should serve as the main title, while the second line functions as a sublabel. In this screenshot provided, you can see where I ...

How can transitions be activated in Bootstrap 4 carousel?

Having an issue with my Bootstrap 4 carousel where I need to move 4 slides at a time. I have tried using the code below: $(this).carousel(4); However, the problem is that the carousel only jumps to that index without showing any transitions. Using $(this) ...

I'm having trouble applying CSS stylings to my components in my React project. What could be the issue causing this?

As a React beginner, I am facing issues with getting my CSS to work properly in my project. I know that CSS is not used directly in React, but rather interpreted as JavaScript using webpack and babel. What have I tried? I attempted to make changes to my w ...

Securing page access with a straightforward password protection using Flask

Looking for a straightforward way to add password protection to a Flask app site. I've explored options like flask_login, but they seem overly complex for my needs. I don't need high security or login sessions - just something like: "enter passw ...

Gathering data from a webpage containing a table populated by JavaScript

I need to extract data from this webpage in order to process generation data with a parser later. The issue I'm facing is that the table on the page is populated by multiple scripts making requests to another server. When using Beautiful Soup to scra ...

Adjustments to the positioning of masonry columns

Hello everyone, I'm in need of some assistance with editing this code. My goal is to make the effect clickable rather than activated by hovering. You can view the current code snippet here. Here is the existing code: $container.find('.shifty-i ...

Encountered an issue retrieving two rows nested within a parent row using Bootstrap 4 and VueJs

Currently, I am working on a VueJs and Bootstrap4 component where my aim is to achieve a design similar to the one shown using the available bootstrap classes. After going through the documentation, I came across the customized classes h-75 and h-25 provi ...

Send the data to be placed in the div to launch the window

I'm attempting to open a new window within the same tab and transfer some html code to be inserted into a specific div section of the newly opened window. However, I'm encountering difficulties in passing the data as illustrated below: JavaScrip ...