question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I want to get images from local url, I did this before with pure js something like this :

    var carte = new google.maps.ImageMapType({
        getTileUrl: function (tileCoord, zoom) {
            return 'http://localhost/' + (zoom + 1) + "/" + (tileCoord.x + 1) + ":" + (tileCoord.y + 1) + "/tile.png";
        }, 
        tileSize: new google.maps.Size(256, 256),  
        minZoom: 8,
    });

how can I override getTileUrl here with react ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
istarkovcommented, Apr 26, 2017

I have no idea why any part of gmap api is not working 😃

1reaction
cresenciofcommented, Jan 8, 2022

This works for me using ImageMapTypes and

  • google-map-react ^2.1.10
  • React 17
  • NextJS 12
import GoogleMapReact from 'google-map-react';
import React, { FC, Fragment, useEffect, useRef } from 'react';

// Normalizes the coords that tiles repeat across the x axis (horizontally)
// like the standard Google map tiles.

type Coord = {
  x: number;
  y: number;
};

const getNormalizedCoord = (coord: Coord, zoom: number): Coord | null => {
  const y = coord.y;
  let x = coord.x;
  // tile range in one direction range is dependent on zoom level
  // 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
  const tileRange = 1 << zoom;

  // don't repeat across y-axis (vertically)
  if (y < 0 || y >= tileRange) {
    return null;
  }

  // repeat across x-axis
  if (x < 0 || x >= tileRange) {
    x = ((x % tileRange) + tileRange) % tileRange;
  }
  return { x: x, y: y };
};

const Marker: FC<{ lat: number; lng: number }> = ({ children }) => (
  <Fragment>{children}</Fragment>
);

const CustomMap: FC = () => {
  const defaultProps = {
    center: {
      lat: 10.99835602,
      lng: 77.01502627,
    },
    zoom: 11,
  };

  const updateMaptile = (map: any, maps: any) => {
    if (!map || !maps) {
      return;
    }
    const moonMapType = new maps.ImageMapType({
      getTileUrl: function (coord: Coord, zoom: number): string {
        const normalizedCoord = getNormalizedCoord(coord, zoom);

        if (!normalizedCoord) {
          return '';
        }

        const bound = Math.pow(2, zoom);

        return (
          'https://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw' +
          '/' +
          zoom +
          '/' +
          normalizedCoord.x +
          '/' +
          (bound - normalizedCoord.y - 1) +
          '.jpg'
        );
      },
      tileSize: new maps.Size(156, 156),
      maxZoom: 9,
      minZoom: 0,
      // @ts-ignore TODO(jpoehnelt) 'radius' does not exist in type 'ImageMapTypeOptions'
      radius: 1738000,
      name: 'Moon',
    });
    map.mapTypes.set('moon', moonMapType);
    map.setMapTypeId('moon');
    map.setZoom(1);
  };


  return (
    <div style={{ height: '70vh', width: '100%' }}>
      <GoogleMapReact
        bootstrapURLKeys={{ key: process.env.NEXT_PUBLIC_MAPS_API_KEY || '' }}
        defaultCenter={defaultProps.center}
        defaultZoom={defaultProps.zoom}
        onGoogleApiLoaded={({ map, maps }) => {
          updateMaptile(map, maps)
        }}
      >
        <Marker {...defaultProps.center}>
          <h1>Hi!</h1>
        </Marker>
      </GoogleMapReact>
    </div>
  );
};

export default CustomMap;

Read more comments on GitHub >

github_iconTop Results From Across the Web

ee.data.getTileUrl - Earth Engine - Google Developers
Generate a URL for map tiles from a Map ID and coordinates. If formatTileUrl is not present, we generate it by using or...
Read more >
GetTileUrl Method
Returns a URL to the specific tile in an ArcGISTiledMapServiceLayer. Syntax. Visual Basic (Declaration). Public Overrides Function GetTileUrl( _ ByVal level ...
Read more >
Extending Leaflet, New Layers - Leaflet - Leaflet
One of them is L.TileLayer.getTileUrl() . This method is called internally by L.TileLayer whenever a new tile needs to know which image to...
Read more >
Class: TileSource - OpenSeadragon
getTileUrl (level, x, y) → {String|function}. Responsible for retrieving the url which will return an image for the region specified by the given...
Read more >
jquery - How to add Authorization header in getTileUrl for ...
I've found a solution by impementing the proxy. Here is the code implemeted : var carte = new google.maps.ImageMapType({ getTileUrl: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found