javascript - How to "generate" unique variables? -
i'm working on google map, , want "generate" pins api. stuck, because pin looks this:
var imagedriver = 'marker.png'; var driverlatlng = new google.maps.latlng(location.lat, location.lng); var drivermarker = new google.maps.marker({ position: driverlatlng, map: map, icon: imagedriver, title: drivers[0].name }); }
i want create function of sort "generate" these variables prefix. check example below:
var imagedriver0; var driverlatlng0; var drivermarker0; position: driverlatlng0, map: map, icon: imagedriver0, title: drivers[o].name
these variables must unique of course don't "collide". have for-loop counts "drivers" inside api. somehow make for-loop create these pins? example:
driversrealtime(); function driversrealtime() { setinterval(function () { var url = "http://blackcab.didair.se/api/drivers"; var lat; var lon; var name; var id; $.getjson(url, function (response) { (var = 0; < response.drivers.length; i++) //counts data-elemements inside lat = response.drivers[i].currentlat; lon = response.drivers[i].currentlon; name = response.drivers[i].name; id = response.drivers[i].profileid; console.log(lat); console.log(lon); console.log(name); console.log(id); }); }, 3000); //delay in milliseconds }
it's possible create variable names dynamically, pointless.
use array instead or suffix, , put values properties in object in array. example:
var points = []; var point = { "url": url, "lat": lat, "lng": lng, "name": name, "id": id }; points.push(point);
Comments
Post a Comment