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.

Change the center of the map

See original GitHub issue

Hi,

I am trying to shift the center of the map a little bit up to make some space for overlay UI, so I used yOffset for that but I get pretty weird results, i.e misplaced markers etc…

I pretty much follow the following example: https://www.react-simple-maps.io/with-react-motion

This is my projection configuration:

projectionConfig = {
  yOffset: -150 / this.state.zoom
}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
pronebirdcommented, Jan 18, 2018

I solved the problem by adjusting the center.

const offset = [0, 150];
const zoomCenter= projection.invert([
  projection(center)[0] + offset[0] / zoom,
  projection(center)[1] + offset[1] / zoom
]);
1reaction
zimrickcommented, Sep 30, 2017

Yeah, center is a geo coordinate that sets the focus of the map. It would achieve a similar result, i.e. moving your map up or down, depending on the coordinate that the map focuses on by default.

I am having trouble recreating the problem with yOffset and the misplaced markers. I tried this and it works. Am I missing something?

class OffsetMap extends Component {
  render() {
    return (
      <div>
        <ComposableMap
          projectionConfig={{
            yOffset: 50,
          }}
          style={{
            border: "1px solid #DDD",
          }}
          >
          <ZoomableGroup zoom={1}>
            <Graticule />
            <Geographies geographyUrl="/static/world-50m.json">
              {(geographies, projection) =>
                geographies.map((geography, i) =>
                  <Geography
                    key={i}
                    geography={geography}
                    projection={projection}
                    style={{ default: { fill: "#DDD" } }}
                  />
              )}
            </Geographies>
            <Markers>
              <Marker marker={{ coordinates: [8.5417,47.3769] }}>
                <circle cx={0} cy={0} r={5} />
              </Marker>
              <Marker marker={{ coordinates: [103.8198,1.3521] }}>
                <circle cx={0} cy={0} r={5} />
              </Marker>
              <Marker marker={{ coordinates: [151.2093,-33.8688] }}>
                <circle cx={0} cy={0} r={5} />
              </Marker>
              <Marker marker={{ coordinates: [-122.4194,37.7749] }}>
                <circle cx={0} cy={0} r={5} />
              </Marker>
              <Marker marker={{ coordinates: [-58.3816,-34.6037] }}>
                <circle cx={0} cy={0} r={5} />
              </Marker>
            </Markers>
          </ZoomableGroup>
        </ComposableMap>
      </div>
    )
  }
}

It gives me a map that looks like this:

screen shot 2017-09-30 at 13 08 01

Note that if you added your own markers and they are misplaced, it may be that the order of longitude and latitude is reversed. I know this is an issue when working with coordinates from google and d3-geo. Google uses [lat,long] and d3-geo uses [long,lat]. React-simple-maps uses the d3-geo order. If this is the case, you would be getting very strange results, but if you are using the motion example with the markers that are included there, this shouldn’t be a problem…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing Map Center - Precisely Help
Changing Map Center · Look for the search box at the top left side of your screen. · Click or touch the search...
Read more >
How to dynamically change the centre of a google maps ...
You can simply move the map with map.setCenter(LatLng) . This method will not use any other markers and will not erase existing markers....
Read more >
Solved: How can I change/adjust the center of Map visual
Solved: HI I want to create a report wtih the Map visual, but the center of the Map is always London just like...
Read more >
How do you get google maps to update search results as you ...
How do you get google maps to update search results as you change the ... You then move the center of the map...
Read more >
Change center of map in OpenLayers dynamically
Is there a way to change the center of the map in OpenLayers dynamically? For example on button click. It would be too...
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