Skip to content

Commit ed0875b

Browse files
authored
Merge pull request #5 from martavj/feature/change-location-map
add a button to change the location
2 parents a8c2694 + 6fa4bdf commit ed0875b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

css/estilo.css

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ body {
1515
#map-button-container {
1616
display: flex;
1717
}
18+
19+
button {
20+
outline: solid 1px black;
21+
}

index.js

+54-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ require([
1515
GraphicsLayer,
1616
Graphic
1717
) {
18-
esriConfig.apiKey = apiKey;
19-
18+
//esriConfig.apiKey = apiKey;
19+
esriConfig.apiKey =
20+
"AAPK87580e590af24ff4a112832c43dbb704_CtPugq6Hs7TGTKzl-hgwybVFmduOYi9arnEQy3D2VJdsZz0Qns4n6tlJRIhtAcE";
2021
// Search widget which includes the locator. We will get the location from the results.
2122
const search = new Search({
2223
container: document.getElementById("search-box"),
@@ -149,5 +150,56 @@ require([
149150
symbol: simpleMarkerSymbol,
150151
});
151152
graphicsLayer.add(pointGraphic);
153+
const buttonsDiv = document.getElementById("buttons-box");
154+
//Add a button to show change the location on the map
155+
const buttonChangeLocation = document.createElement("button");
156+
buttonChangeLocation.innerHTML = "Cambiar localización";
157+
buttonsDiv.appendChild(buttonChangeLocation);
158+
//If we click on 'Cambiar localización', the text changes giving the instructions: click on the map to change the location.
159+
buttonChangeLocation.onclick = function () {
160+
changeLocation(
161+
graphicsLayer,
162+
pointGraphic,
163+
buttonChangeLocation,
164+
view,
165+
simpleMarkerSymbol
166+
);
167+
};
168+
};
169+
170+
changeLocation = (
171+
graphicsLayer,
172+
pointGraphic,
173+
buttonChangeLocation,
174+
view,
175+
simpleMarkerSymbol
176+
) => {
177+
console.log("ChangeLocation");
178+
graphicsLayer.remove(pointGraphic);
179+
buttonChangeLocation.innerHTML =
180+
"Haz click en el mapa para cambiar la localización.";
181+
//If we click on the map, we capture the coordinates and do the reserve geocoding.
182+
view.on("click", function (event) {
183+
// Get the coordinates of the click on the view
184+
//Create a new point using the new coordinates
185+
const point = {
186+
type: "point",
187+
longitude: event.mapPoint.longitude,
188+
latitude: event.mapPoint.latitude,
189+
};
190+
191+
const newLocationLongitude = event.mapPoint.longitude;
192+
const newLocationLatitude = event.mapPoint.latitude;
193+
//Create the graphic for the point
194+
const pointGraphic = new Graphic({
195+
geometry: point,
196+
symbol: simpleMarkerSymbol,
197+
});
198+
//Add new graphic to the map
199+
graphicsLayer.add(pointGraphic);
200+
buttonChangeLocation.innerHTML = "Cambiar localización";
201+
view.goTo({ center: [newLocationLongitude, newLocationLatitude] });
202+
view.zoom = 16;
203+
});
152204
};
153205
});

0 commit comments

Comments
 (0)