0

I was trying to have points plotted on a map based on the address for a project but for right now while testing I am just putting the map's view on the location. I found some information online giving a function to do this and it seemed to work with an older version of the library but on top of that it gave me the error "Should not import the named export 'version' (reexported as 'VERSION') from default-exporting module (only default export is available soon)".

I decided to update the library to the latest version and now I am getting this error "TypeError: Cannot read properties of undefined (reading '0')" which after some debugging showed me that it was returning null. I have no idea if there is a way to get rid of either error anything helps. It seemed that what I was trying to do was working with the older version but the error was showing up on top of it anyway here is my code.

import {MapContainer, Marker, Popup, TileLayer, useMap} from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "leaflet-geosearch/dist/geosearch.css";
import * as ELG from 'esri-leaflet-geocoder';

const MapWrapper = () => {

 function Geocoder({ address }) {
        const map = useMap();

        ELG.geocode()
            .text(address)
            .run((err, results, response) => {
                console.log(results.results[0].latlng);
                const { lat, lng } = results.results[0].latlng;
                map.setView([lat, lng], 12);
            });

        return null;
    }

  return (
            <div id="mapid">
                <MapContainer
                    center={[40.730610,-73.935242]}
                    zoom={13}
                    scrollWheelZoom
                    style={{ height: "500px", borderRadius: '10px' }}
                >
                    <TileLayer
                        attribution='Google Maps'
                        url="https://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}"
                    />
                    //Problem here 2.3.4 and 3.1.4
                    <Geocoder address={'nyc'}/>
                </MapContainer>
            </div>

        );
    }

export default MapWrapper;


I was trying to figure out why there is nothing in the array from the docs but there doesn't seem to be anything explaining why .geocode doesn't work. It is still shown in the docs but doesn't get recognized when I try to use it in the newer version. I'm fine with using the older version as it does what I need to but I need to know how to get rid of that pesky "Should not import the named export 'version' (reexported as 'VERSION') from default-exporting module (only default export is available soon)".

adigrigs
  • 1
  • 2
  • Update I was able to solve the problem on the new version by adding an API key. Nowhere in the docs did it ever say that it was a change was made to require that. I would still appreciate if anyone had a solution for the error on the old version as I rather use that one as it doesn't require an API key so I wouldn't have to pay after a certain amount of requests. – adigrigs Apr 27 '23 at 05:45

1 Answers1

0

You can implement a geosearch without the Esri Leaflet Geocoder and ArcGIS, as you can test in the simple jsFiddle demo:

animated open street map with geocoder search

The Javascript code below does not require any API key or payments:

'use strict';

var myMap = L.map('mapId').setView([40.730610,-73.935242], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors'
}).addTo(myMap);

var search = new GeoSearch.GeoSearchControl({
  provider: new GeoSearch.OpenStreetMapProvider(),
  style: 'bar',
  showPopup: true,
  popupFormat: ({
      query,
      result
    }) =>
    result.label +
    ': longitude: ' + parseFloat(result.x).toFixed(6) +
    ', latitude: ' + parseFloat(result.y).toFixed(6)
});

myMap.addControl(search);
html, body { 
  margin: 0;
  padding: 0;
}

#mapId {
  position: absolute;
  width: 100%;
  height: 100%;
}
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.min.css">
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet-geosearch@3/dist/geosearch.min.css">
<script src="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet-src.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/leaflet-geosearch@3/dist/bundle.min.js"></script>

<div id="mapId"></div>
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • While that would have been useful if I was using a search bar, I’m plotting the points based on address data from a mongodb database. I didn’t make that clear that’s my fault. Is there a free way to geocode addresses you know of similar to the search bar provided? If you’d like to get in contact I’d be open to chatting. I just want to get this project done :’( – adigrigs Apr 27 '23 at 17:15
  • Install your own [Nominatim geocoder](https://nominatim.org/release-docs/latest/admin/Installation/) and resolve the addresses against it – Alexander Farber Apr 27 '23 at 17:33
  • I was able to do it for free using something similar to from this link. Thanks again I appreciate the help. https://dev.to/prasannaemmadi/how-to-use-leaflet-control-geocoder-with-react-5e21 – adigrigs Apr 27 '23 at 20:30