1

I'm using the jquery ui map in my cordova iOS app, and need to encode all my database results (latitudes and longitudes) in JSON. I already set the localstorage element, and know I received the correct response, but I dont know how to encode all the results into a JSON array. I've tried using a for loop, but I must have done it incorrectly because my code then failed. One more thing, I know that the map works, and also that adding markers work, but I used the demo json array. I need to create my own array with my own objects, and dont know how to. Thanks for the help.

MAP.HTML

    <script type="text/javascript">
        $(document).ready(function() {
                $('#map_canvas').gmap().bind('init', function(evt, map) {
                    $('#map_canvas').gmap('getCurrentPosition', function(position, status) {
                        if ( status === 'OK' ) {
                            var clientPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                            $('#map_canvas').gmap('addMarker', {'position': clientPosition, 'bounds': true});
                            $('#map_canvas').gmap('addShape', 'Circle', {
                                'strokeWeight': 0,
                                'fillColor': "#008595",
                                'fillOpacity': 0.25,
                                'center': clientPosition,
                                'radius': 15,
                                'clickable': false
                            });
                        }
                    });
                });
                var name = localStorage.getItem("name");
                var lat = localStorage.getItem("lat");
                var lon = localStorage.getItem("lon");
                var it = localStorage.getItem("it");

                alert(lat);
            });
        </script>
copilot0910
  • 431
  • 2
  • 6
  • 19
  • Do you really want to create JSON or do you want to create an array (or object)? JSON is data format that is typically used to persist data or send data over the network, while the latter are data types in JavaScript. – Felix Kling Feb 25 '13 at 01:36
  • Yes, I need to create a JSON array because the jquery ui map will only ready data encoded with json. I understand completely why you would ask though – copilot0910 Feb 25 '13 at 01:39
  • Ok. I'm not sure what exactly you want to create, but arrays are explained [here](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Array_Object), objects [here](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects) and how to convert them to JSON [here](http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string). – Felix Kling Feb 25 '13 at 10:46

1 Answers1

1

You need to convert your JSON object to string and store the string.
Use JSON.stringify(yourObject) to convert to string and JSON.parse([your Object from localstorage]).
See similar question

Community
  • 1
  • 1
NickF
  • 5,637
  • 12
  • 44
  • 75
  • Thanks for the response, but I actually solved this question much earlier on. Even so, I did use a similar method, so you did answer the question. Hopefully others can use your answer too. – copilot0910 Aug 22 '13 at 17:30