对于大多数人来说这可能很容易,但我真的无法弄清楚我的代码可能出现什么问题,我觉得我尽可能地尝试了一切。 基本上我想要做的是对字符串地址进行地理编码,并将结果存储在我的数据库中。 我设法让地理编码部分工作,因为它发回纬度和经度。 现在,我想将结果存储回我的数据库中。 通常我使用this.set('myfield',myvar)但myvar设置在“if”中,看起来它阻止我这样做。 我能做什么 ? 这是我的控制器“actions”部分中的代码:
geocode(location, mylat, mylng) { const google = window.google; var geocoder = new google.maps.Geocoder(); var latlng = ""; var lat = ""; var lng = ""; geocoder.geocode( { 'address': location }, function(results, status) { if (status == google.maps.GeocoderStatus.OK && results.length > 0) { latlng = results[0].geometry.location; lat = latlng.lat(); lng = latlng.lng(); alert(lat); // it works alert(lng); // it works too this.set(`model.lat`, latlng); //not working here } } ); alert(latlng); // not working this.set(`model.lat`, latlng); //not working too },我花了大概8个小时试图解决这个问题而且我完全迷失了,所以任何帮助都会非常感激。 提前谢谢了,
this is probably very easy for most you, but I really can't figure out what could be wrong with my code and I feel like I tried everything possible. Basically what I want to do, is to geocode a string adress, and store the result in my database. I managed to get the geocode part to work, as it sends back the latitude and longitude. Now, I would like to store the result back in my database. Usually I use this.set('myfield', myvar) but myvar is set inside an "if" and it looks like it prevent me to do that. What can I do ? Here is my code inside the "actions" section of my controller:
geocode(location, mylat, mylng) { const google = window.google; var geocoder = new google.maps.Geocoder(); var latlng = ""; var lat = ""; var lng = ""; geocoder.geocode( { 'address': location }, function(results, status) { if (status == google.maps.GeocoderStatus.OK && results.length > 0) { latlng = results[0].geometry.location; lat = latlng.lat(); lng = latlng.lng(); alert(lat); // it works alert(lng); // it works too this.set(`model.lat`, latlng); //not working here } } ); alert(latlng); // not working this.set(`model.lat`, latlng); //not working too },I spent maybe 8 hours trying to solve this and I'm totally lost, so any help would be sincerely greatly appreciated. Many thanks in advance,
最满意答案
在geocode方法的回调中,没有定义它。
这里是geocoder.geocode( { 'address': location }, function(results, status)
您可以像这样保持this的参考:
actions: { geocode(location, mylat, mylng){ const google = window.google; var geocoder = new google.maps.Geocoder(); var latlng = ""; var lat = ""; var lng = ""; const self = this; geocoder.geocode({'address': location}, function(results, status) { if (status == google.maps.GeocoderStatus.OK && results.length > 0) { latlng = results[0].geometry.location; lat = latlng.lat(); lng = latlng.lng(); alert(lat); // it works alert(lng); // it works too self.set(`model.lat`, latlng); //not working here } }); alert(latlng); // not working this.set(`model.lat`, latlng); //not working too }In the callback of geocode method, the this is not defined.
Here geocoder.geocode( { 'address': location }, function(results, status)
You can keep a ref to this like this :
actions: { geocode(location, mylat, mylng){ const google = window.google; var geocoder = new google.maps.Geocoder(); var latlng = ""; var lat = ""; var lng = ""; const self = this; geocoder.geocode({'address': location}, function(results, status) { if (status == google.maps.GeocoderStatus.OK && results.length > 0) { latlng = results[0].geometry.location; lat = latlng.lat(); lng = latlng.lng(); alert(lat); // it works alert(lng); // it works too self.set(`model.lat`, latlng); //not working here } }); alert(latlng); // not working this.set(`model.lat`, latlng); //not working too }
更多推荐
发布评论