I would like to be able to have my map centered at a pre-entered location. For example:
Page 1: user submits a location and gets redirect to page 2
Page 2: takes input from page 1 and centers map based off of the location
Assuming input from page 1 is saved into variable 'location', I've tried:
<script src="/static/js/leaflet.js" type="text/javascript"></script>
<script src="/static/js/l.control.geosearch.js" type="text/javascript"></script>
<script src="/static/js/l.geosearch.provider.google.js" type="text/javascript"></script>
<script type="text/javascript">
var googleGeocodeProvider = new L.GeoSearch.Provider.Google();
var map = L.map('map', {center: [30.27, -97.75], zoom: 10, maxZoom: 18, minZoom: 3});
L.tileLayer(''http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'}).addTo(map);
var geosearch = new L.Control.GeoSearch({
provider: googleGeocodeProvider
}).addTo(map);
googleGeocodeProvider.GetLocations(location, function (data) {
console.log(data); # according to docs (https://github.com/smeijer/L.GeoSearch), data should have the x,y info I'm looking for
});
The above code returns the error:
Uncaught TypeError: Cannot read property 'geocode' of undefinedL.GeoSearch.Provider.Google.L.Class.extend.GetLocations @ l.geosearch.provider.google.js:39(anonymous function) @ (index):357
Edit:
Sorry Ghybs, I should have been more clear about the issue that I had. I am using django to service web requests and I'm able to pass variables between pages easily. The issue was that I was not able to call geosearch outside of the leaflet map with a given query and have it return the coordinates I need to re-center the map.