0

I need to allow the user to change the extent while the Locator Widget is tracking. I defined the locator widget with the following code

var locateButtonLayer = new GraphicsLayer();
app.map.addLayer(locateButtonLayer);
var geoLocate = new LocateButton({
    map: app.map,
    graphicsLayer: locateButtonLayer,
    useTracking: true,
    setScale: false,
    centerAt: false
}, "LocateButton");
geoLocate.startup();
app.geoLocate = geoLocate;

If I don't set setScale and centerAt to false some browsers will reset the extent every few seconds. If you user wants to re-center on their location the following code is called.

function customLocate() {
   require(["dojo/Deferred"], function(Deferred) {
       app.map.setScale(9000).then(function() {
           app.geoLocate.centerAt = true;
           app.geoLocate.locate().then(function(result) {
               app.geoLocate.centerAt = false;
           });
       });
   });      

}

My issue is the locate() method will only center on the user's extent if I set a breakpoint on the app.geoLocate.centerAt = false; line. Setting a breakpoint on any other line does not help. I assume this has to do with the locate() returning a promise but I can't figure out how to fix the issue. The code appeared to run fine when I put it in the Locator Widget example sandbox at Locator Widget Sandbox

EDIT: The code works fine if I add an alert as shown below:

function customLocate() {
   require(["dojo/Deferred"], function(Deferred) {
       app.map.setScale(9000).then(function() {
           app.geoLocate.centerAt = true;
           app.geoLocate.locate().then(function(result) {
               alert("here");
               app.geoLocate.centerAt = false;
           });
       });
   });      

}

EDIT: I tried the following:

  app.map.setScale(9000).then(function() {
   app.geoLocate.centerAt = true;
    on(app.map, "update-end", function(){
        app.geoLocate.locate().then(function() {
            on(map, "update-end", function(){
                app.geoLocate.centerAt = false;
            });
        });
    });

});

This solved the issue with the locate() method executing before the map was done updated but the app.geoLocate.centerAt = false line never gets executed so the map keeps re-centering every few seconds. Evidently the locate() method prevents the update-end event from occurring when it is in tracking mode.

  • So it seems like you want to allow the user to turn on the LocateButton without automatically centering the map initially, but have a button that will do a 1-time center-and-zoom into the current location? – GavinR May 16 '18 at 15:19
  • The user wants the first click on the icon to center-and-zoom and start the tracking. They then want to be able to look at different parts of the map while the tracking is going on without center-and-zooming every time their location changes. If they click on the icon after the tracking starts they want to center-and-zoom to their current location. That is how my mapquest mobile app works. – ponder275 May 16 '18 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.