Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map doesn't re-render when lat/long input is updated via javascript #137

Open
MikeSandford opened this issue Nov 2, 2019 · 6 comments
Open

Comments

@MikeSandford
Copy link

MikeSandford commented Nov 2, 2019

I got the app installed and the documentation so far as been pretty good! I'm stoked to be able to use something like this.

What I'm trying to do is call out to the browser's geolocation API and get the location that way. Not all that useful for a desktop but when using on a phone or a tablet it means that you can tag exactly where you're gathering data.

{{form.media}}
<script type="text/javascript">
$("#find_btn").click(function (){
	if ("geolocation" in navigator){
		navigator.geolocation.getCurrentPosition(function(position){
      var loc_string = position.coords.latitude + "," + position.coords.longitude;
      console.log("Found location as " + loc_string);
      //$("#id_location").fill(position.coords);
      $("#id_location").val(loc_string);
      $("#id_location").trigger( "keyup" );
      //$("#map_location").render();
      //console.log($("#map_location"));
		});
	}else{
		console.log("Browser doesn't support geolocation!");
	}
});
</script>

I've got the code handled that calls out to the browser (following this tutorial: https://www.sanwebe.com/2016/04/get-current-location-of-user-using-jquery) and it even updates the input! But the input being updated doesn't seem to cause the map to re-render. I'm finding digging into the code difficult to do as the patterns are a bit unfamiliar to me. I tried sending the input a keyup event (based on the event handlers I saw being wired up) but I think those are for watching the based_fields inputs PlainLocationField(based_fields=['city']

Is it possible for me to cause the map to get re-rendered? Obviously it'd be super awesome if the library did this by default whenever the lat/long input changed. But I'm more than happy to figure out what function I can call on the input or the map to get it to be re-rendered as I'm fine making a user press a button to get a new geolocation sample taken and have the map updated in that case only.

@MikeSandford MikeSandford changed the title Map doesn't re-render when location field input is updated via javascript Map doesn't re-render when lat/long input is updated via javascript Nov 2, 2019
@caioariede
Copy link
Owner

@MikeSandford did you try trigger change event?

@MikeSandford
Copy link
Author

@caioariede thanks for taking a look at this! I just tried change, keyup, focusout and blur.

$("#id_location").val(loc_string).change().focusout().blur().keyup();

I also tried manually inputting 0,0 and when I clicked away from the input the map and marker didn't update that way either. It seems as though there's a binding from the "watched" form inputs to the map, but not from the actual lat/lon input to the map.

@MikeSandford
Copy link
Author

@caioariede I was able to put together some code that does the latlong input field to map marker binding and it works on my local machine. But then I went down the rabbit hole a little bit looking at leaflet docs and it seems like there's stuff in leaflet to do exactly that! map.locate is available and you can even pass in a watch=true parameter.

I can make a PR with what I already have but I'm not entirely sure what's the best route forward here. Are you interested in adding this capability? If so do you have a preference on how it's achieved? The one difficulty I have on attempting to expose the leaflet functionality is that I'm not sure how to get a reference to the map from the context of a webpage that has a button on it that a user presses to center the map on themselves. I can find the jQuery node that's the div of the map widget or the inner div of the actual rendered map but I haven't figured out how to get something that I can then call any of the $.locationField methods on.

@Alsaheem
Copy link

hello @MikeSandford please im havin gthe same issue , how do i fix it ,....

Thanks

@pfcodes
Copy link

pfcodes commented Jan 20, 2023

+1

@michaelhjulskov
Copy link
Contributor

michaelhjulskov commented Mar 2, 2023

Solution would be to trigger a change on the city field or one of the other fields, "its" listening to.

Example if you have jQuery

after you have changed the value of location input field - then trigger a change on the city field

loc_string = ?something?
$("#id_location").val(loc_string);
$("#id_city").trigger('change');

or maybe you should trigger keyup on the city field - you'll have to look in the code to see or just play around with it.

I think that will do the trick for you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants