I get the problem. That is not valid JSON. 
You should not generate JSON. 
response.json(locations) will generate the JSON.

def get_locations():
    events = db(db.events.id>0).select()
    locations = map(lambda event: {
                  'title': event.title, 
                  'link': URL('default', 'mtb_tour', vars=dict(event_id=
event.id)),
                  'location':{'lat':event.latitude , 'lng':event.longitude}
                  }, events)
    return response.json(locations)

On Tuesday, 6 September 2016 10:22:37 UTC-5, Gael Princivalle wrote:
>
> Javascript error is: Uncaught TypeError: Cannot read property 'lat' of 
> undefined
>
> Do I have to add these '[' at the beginning and '']' at the end like in 
> javascript?
> Anyway with or without the error is the same.
>
> Here is the get_locations result:
> "[{title: 'In collina 26 a salire e 600 a scendere',link: 
> '/tour?event_id=27',location: {lat: 45.07540059617286, lng: 
> 7.729324682861261}},{title: 'Giro sul Musin\u00e9',link: 
> '/tour?event_id=26',location: {lat: 45.10739452807225, lng: 
> 7.481788977294855}},{title: 'Tour de la Roccasera',link: 
> '/tour?event_id=25',location: {lat: 43.86517295729843, lng: 
> 7.2934039721833415}},{title: 'Fortezza Chaberton',link: 
> '/tour?event_id=28',location: {lat: 44.9818161, lng: 
> 6.798535300000026}},{title: 'Giro cittadino a Torino',link: 
> '/tour?event_id=7',location: {lat: 45.070309, lng: 
> 7.686580999999933}},{title: 'Giro nel Canavese',link: 
> '/tour?event_id=12',location: {lat: 45.24897009652526, lng: 
> 7.71885333886712}},]"
>
> Il giorno martedì 6 settembre 2016 16:42:24 UTC+2, Massimo Di Pierro ha 
> scritto:
>>
>> definitively you want 
>>
>> def get_locations():
>>     return response.json(locations)
>>
>> and 
>>
>> $.getJSON instead of $.get. 
>>
>> which those changes what errors do you get?
>>
>> On Monday, 5 September 2016 15:51:44 UTC-5, Gael Princivalle wrote:
>>>
>>> Thanks Dave.
>>>
>>> However I'm still having trouble.
>>>
>>> With that script in the view google maps API load my all locations:
>>> I build a Javascript object.
>>>             var locations = [
>>>                 {{for event in events:}}
>>>                     {title: '{{=event.title}}',
>>>                       link: '{{=URL('default', 'mtb_tour', 
>>> vars=dict(event_id=event.id))}}',
>>>                       location: {lat: {{=event.latitude}}, lng: {{=event
>>> .longitude}} }},
>>>                 {{pass}}
>>>             ];
>>>
>>> What I would like to do it to update theses locations when the user 
>>> change the map view.
>>> So when the map is in idle I would like to get all locations from the 
>>> new bounds. I've not set yet the new bounds test with getBounds()as I'm 
>>> still in trouble for getting data. 
>>>             map.addListener("idle", function(){
>>>                 hideListings();
>>>                 $.get("{{=URL('default', 'get_locations')}}", function(
>>> locations,status) {
>>>                     //alert("Data: " + locations + "\nStatus: " + 
>>> status);
>>>                     for (var i = 0; i < locations.length; i++) {
>>>                         // Get the position from the location array.
>>>                         var position = locations[i].location;
>>>                         var title = locations[i].title;
>>>                         var link = locations[i].link;
>>>                         // Create a marker per location, and put into 
>>> markers array.
>>>                         var marker = new google.maps.Marker({
>>>                             position: position,
>>>                             title: title,
>>>                             link: link,
>>>                             animation: google.maps.Animation.DROP,
>>>                             icon: defaultIcon,
>>>                             id: i
>>>                         });
>>>                         // Push the marker to our array of markers.
>>>                         markers.push(marker);
>>>                         // Create an onclick event to open the large 
>>> infowindow at each marker.
>>>                         marker.addListener('click', function() {
>>>                             populateInfoWindow(this, largeInfowindow);
>>>                         });
>>>                         // Two event listeners - one for mouseover, one 
>>> for mouseout,
>>>                         // to change the colors back and forth.
>>>                         marker.addListener('mouseover', function() {
>>>                             this.setIcon(highlightedIcon);
>>>                         });
>>>                         marker.addListener('mouseout', function() {
>>>                             this.setIcon(defaultIcon);
>>>                         });
>>>                     }
>>>                     showListings();
>>>                 });
>>>             });
>>>
>>>
>>> Here is in my controller the get_locations function:
>>> def get_locations():
>>>     events = db(db.events.id>0).select()
>>>     locations= '['
>>>     for event in events:
>>>         locations += "{title: '" + event.title + "',"
>>>         locations += "link: '" + URL('default', 'mtb_tour', vars=dict(
>>> event_id=event.id)) + "',"
>>>         locations += "location: {lat: " + event.latitude + ", lng: " + 
>>> event.longitude + "}},"
>>>     locations+= ']'
>>>     return locations
>>>
>>> Like that it return a string, and not a Javascript object.
>>> So the Javascript error is 'Cannot read property 'lat' of undefined'
>>>
>>> I've tried to communicate also with Json. If I've understand it's a data 
>>> format like in Javascript.
>>> If I substitute in the javascript:
>>> $.get
>>> by
>>> $.getJson
>>>
>>> and in the controller:
>>> return locations
>>> by
>>> return response.json(locations)
>>>
>>> Javascript error still the same.
>>>
>>> Can someone give me a hand?
>>>
>>> Thanks.
>>>
>>> Il giorno sabato 6 agosto 2016 01:14:43 UTC+2, Dave S ha scritto:
>>>>
>>>>
>>>>
>>>> On Friday, August 5, 2016 at 2:26:51 PM UTC-7, Gael Princivalle wrote:
>>>>>
>>>>> Hello.
>>>>>
>>>>> I would like to update a javascript array by ajax, keeping the data 
>>>>> into the db.
>>>>> We talk about markers data for Google maps.
>>>>>
>>>>> Here is how I load the data when the page is loaded.
>>>>>         var locations = [
>>>>>             {{for event in events:}}
>>>>>                 {title: '{{=event.title}}',
>>>>>                   link: '{{=URL('default', 'show_event', vars=dict(id=
>>>>> event.id))}}',
>>>>>                   location: {lat: {{=event.latitude}}, lng: {{=event.
>>>>> longitude}} }},
>>>>>             {{pass}}
>>>>>         ];
>>>>>
>>>>> If the user change in a form some criteria like event date or event 
>>>>> type, I would like to update the markers dynamically, without a page 
>>>>> reload.
>>>>>
>>>>> Can I do it by ajax?
>>>>>
>>>>> Thanks.
>>>>>
>>>>>
>>>> Are you trying to  update the client, or update the server?  In either 
>>>> case, the answer seems to be yes.  In addition to the LOAD helper, there's 
>>>> some conversations around here about jquery.  I thought I had seen a more 
>>>> direct answer recently, but I can't put my finger on it.  But have a look 
>>>> at 
>>>>
>>>> <URLhttps://groups.google.com/d/msg/web2py/m0wrV8fK4dE/7Uhw7Vs8AwAJ>
>>>>
>>>> Massimo's discussion of Collection+JSON might be helpful, too.
>>>> <URL:https://groups.google.com/d/msg/web2py/WN9yzLIfi6M/mFEAOySVSH4J>
>>>>
>>>> /dps
>>>>
>>>>
>>>>
>>>>
>>>>  
>>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to