Retrieve data from mapbox geocoder - mapbox

i only need the mapbox geocoding autocomplete without the map (to put the result with lat/lng in another request)
I managed to put it totally alone without the map using this :
<template>
<div id='geocoder' class='geocoder'></div>
</template>
<script>
import MapboxGeocoder from '#mapbox/mapbox-gl-geocoder'
require('../../node_modules/#mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css')
mapboxgl.accessToken = '<your access token here>';
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken
placeholder: 'Rechercher'
});
document.getElementById('geocoder').appendChild(geocoder.onAdd());
</script>
But now i would like to retrieve the data (specifically the lat/lng attribute in order to save it in my component and work with it)
How can i do that ? i've search through mapbox doc but did not found anything about that :/
Thanks in advance to the community

From the API docs https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on you can use
geocoder.on('results', function(results) {
console.log(results);
})

I've finally found a cheaper and easier to integrate alternative :
https://community.algolia.com/places/documentation.html#using-npm
here is the simple code :
<template>
<input type="search" id="address-input" placeholder="Where are we going?" />
</template>
<script>
import Places from 'places.js'
...
mounted: function() {
var placesAutocomplete = new Places({
container: document.querySelector('#address-input')
})
placesAutocomplete.on('change', e => console.log(e.suggestion))
}
</script>

Related

How to place leaflet geocoder control on a page where there is no map?

I have a project with React where I use Leaflet to render a map. I use leaflet geocoder to look up locations on the map. I want to show the search box on the homepage too where there is no map, but when people choose some place, it will take them to that location on the map. The only problem I have is all the geocoder libraries on leaflet work only when the control is tied to the map. Is there anyway to make it independent from the map?
After some research, I found out leaflet-geosearch library (here) has the ability to be used without being embedded in a map. You can embed it in any form, without few lines of code. I'm using React.
import React, {useState} from 'react'
import {OpenStreetMapProvider} from 'leaflet-geosearch'
const SearchBox = () => {
const provider = new OpenStreetMapProvider();
const [results, setResults] = useState([])
const handleSubmit = async (e) => {
e.preventDefault()
const results = await provider.search({ query: text });
setSearchResult(results)
}
return (
<>
<form onSubmit={handleSubmit}>
<input type="text" value={text} onChange={(e) => setText(e.target.value)}
</form>
<div>
{searchResults.map(item => <p>{item.label}</p>}
</div>
</>
)
}
export default SearchBox;
I hope this helps someone in the future.

How to use filter-collections with meteor

I'm trying to use the package doctorpangloss:filter-collections in my project.
What I have now is the following:
Server:
Meteor.publish('locations', function() {
return Locations.find();
});
Router:
this.route('foci', {
path: '/foci',
(...)
},
waitOn : function () {
return Meteor.subscribe('locations');
}
});
Client:
----.html file----
<template name="foci">
<div class="row">
{{> LocationList}}
</div>
</template>
<template name="LocationList">
{{#each locationData}}
(...)
{{/each}}
</template>
----.js file----
Template.LocationList.helpers({
locationData: function(){
return Locations.find()
}
});
Everything standard till now. On the /foci page the data is displayed.
But I would like to have a list (or actually not a list, but tiles, but thats irrelevant here) that I can filter. I've tried my best with this package, but nothing worked. Based on the documentation ( https://github.com/workpop/filter-collections/ ) it should be a few minutes work to get it done, yet I've spent way more on trying to figure it out.
If anyone could help adding the standard filter bar to the page, or knows of another similar package I could use it would be appreciated.

YUI - get node from 'this' keyword

How can I select YUI node with this keyword?
Example:
<a href='javascript:test(this);'>Click Me!</a>
<script>
function test(el){
YUI().use('transition', function (Y) {
var selectedElement = Y.one(el);
});
}
</script>
Thanks a lot for help.
In your code, "this" will refer to the Window object, not the link. A better way to do this in YUI is:
Click Me!
<script>
YUI().use('transition', function(Y) {
Y.one('#my-link').on('click', function(e) {
e.halt();
var selectedElement = e.target;
});
});
</script>
This avoid JavaScript embedded in the markup, which is considered best practice. Also, it wraps all your code in the YUI sandbox, which is the recommended way.

loading leaflet map in ajax loaded page renders no tiles?

Hi I´m doing a phonegap app that loads my subpages with ajax and in one off them I´m trying to load a leaflet map.
It is not rendering the tiles?
I don´t know what I´m missing?
I load the leaflet css and js file in my index file and in my subpage that should display the map I have the following code:
<div id="themappage">
<div id="header" class="toolbar">
<h1>The Map</h1>
BACK
</div>
<div id="map"></div>
<script>
$(document).ready(function(){
var map = L.map('map');
L.tileLayer('http://{s}.tile.cloudmade.com/42dfb943872a465d89807eb88f6a1f4d/997#2x/256/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © CloudMade'
}).addTo(map);
function onLocationFound(e) {
var radius = e.accuracy / 2;
L.marker(e.latlng).addTo(map)
.bindPopup("You are within " + radius + " meters from this point").openPopup();
L.circle(e.latlng, radius).addTo(map);
}
function onLocationError(e) {
alert(e.message);
}
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({setView: true, maxZoom: 16});
});
</script>
</div>
Any input appreciated, thanks.
Update!
Just found out if I use http://{s}.tile.osm.org/{z}/{x}/{y}.png instead off http://{s}.tile.cloudmade.com/42dfb943872a465d89807eb88f6a1f4d/997/256/{z}/{x}/{y}.png as the tile layer then it renders the tiles, why doesn´t the tiles from cloudemade render?
When I tried to load it, I got a 403 (Forbidden) error using that API-Key. Try to get another one (you did request your own right?). The reason that the OSM works is that it doesn't require an API-Key.
The API-Key is the part of the url that starts with 42dfb... and goes to the /. Replace that with a good key and you should be good to go.

Getting mapQuest error: map.addControl is not a function

I've loaded the MapQuest JavaScript api module and am able to bring in the basic sample map at http://developer.mapquest.com/web/documentation/sdk/javascript/v7.0/basic-map but when I try to add map controls using their next example I get the JavaScript error:
map.addControl is not a function
I tried window.map.addControl but that generates the same error.
Does anyone know what might be wrong?
Thanks
You need to add the map control code within your MQA.EventUtil.observe
right after your call to window.map = new MQA.TileMap(options);
Code:
<html>
<head>
<script src="http://www.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?key=Kmjtd%7Cluua2qu7n9%2C7a%3Do5-lzbgq"></script>
<script type="text/javascript">
MQA.EventUtil.observe(window, 'load', function() {
/*Create an object for options*/
var options={
elt:document.getElementById('map'), /*ID of element on the page where you want the map added*/
zoom:10, /*initial zoom level of map*/
latLng:{lat:39.743943, lng:-105.020089}, /*center of map in latitude/longitude*/
mtype:'map' /*map type (map)*/
};
/*Construct an instance of MQA.TileMap with the options object*/
window.map = new MQA.TileMap(options);
MQA.withModule('largezoom','traffictoggle','viewoptions','mousewheel', function() {
map.addControl(
new MQA.LargeZoom(),
new MQA.MapCornerPlacement(MQA.MapCorner.TOP_LEFT, new MQA.Size(5,5))
);
map.addControl(new MQA.TrafficToggle());
map.addControl(new MQA.ViewOptions());
map.enableMouseWheelZoom();
});
});
</script>
</head>
<body>
<div id='map' style='width:750px; height:280px;'></div>
</body>
</html>
try like this map.current.addControl
Make sure you add the module for the control before adding the control to the map.