Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 5.68 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Showing multiple markers on map : Titanium
  2. var path = Ti.Platform.name == 'android' ? Ti.Filesystem.resourcesDirectory : "../../";
  3.  
  4. var map = {
  5.  
  6.     top: 0,
  7.     bottom: 0,
  8.     latitude: 0,
  9.     longitude: 0,
  10.     latitudeDelta: 0.1,
  11.     longitudeDelta: 0.1,
  12.     display: "map",
  13.  
  14.     init: function (annotations, latitude, longitude, top, bottom, delta) {
  15.  
  16.         if (top)
  17.             map.top = top;
  18.         if (bottom)
  19.             map.bottom = bottom;
  20.         if (delta) {
  21.             map.latitudeDelta = delta;
  22.             map.longitudeDelta = delta;
  23.         }
  24.  
  25.         map.createMap(annotations, latitude, longitude);
  26.         map.createOptions();
  27.         map.getLocation();
  28.  
  29.     },
  30.  
  31.     createMap: function (annotations, latitude, longitude) {
  32.  
  33.         map.mapView = Ti.Map.createView({
  34.             mapType: Ti.Map.STANDARD_TYPE, animate: true, regionFit: false, userLocation: true,
  35.             region: { latitude: latitude, longitude: longitude, latitudeDelta: map.latitudeDelta, longitudeDelta: map.longitudeDelta },
  36.             annotations: annotations, bottom: map.bottom, top: map.top, borderWidth: 1
  37.         });
  38.         if (!isAndroid) {
  39.             map.mapView.addAnnotation(annotations[0]);
  40.         }
  41.         map.mapView.selectAnnotation(annotations[0]);
  42.         win.add(map.mapView);
  43.  
  44.     },
  45.  
  46.     createOptions: function () {
  47.  
  48.         //map/satellite displays.
  49.         var mapDisplay = new ImageView({ image: path + 'images/map/satellite-view.png', width: 70, height: 49, zIndex: 2, top: map.top + 5, right: 5 });
  50.         mapDisplay.addEventListener('click', function () {
  51.             if (map.display == "map") {
  52.                 map.mapView.setMapType(Titanium.Map.SATELLITE_TYPE);
  53.                 mapDisplay.image = path + "images/map/map-view.png";
  54.                 map.display = "satellite";
  55.             }
  56.             else {
  57.                 map.mapView.setMapType(Titanium.Map.STANDARD_TYPE);
  58.                 mapDisplay.image = path + "images/map/satellite-view.png";
  59.                 map.display = "map";
  60.             }
  61.         });
  62.         win.add(mapDisplay);
  63.  
  64.         //crosshairs.
  65.         if(Ti.Geolocation.locationServicesEnabled) {
  66.             var centerDisplay = new ImageView({ image: path + 'images/map/crosshairs.png', width: 49, height: 49, zIndex: 2, top: map.top + 5, right: 80 });
  67.             centerDisplay.addEventListener('click', function () {
  68.                 if(map.latitude != 0 && map.longitude != 0) {
  69.                     info("setting user location to " + map.latitude + " / " + map.longitude);
  70.                     //center map.
  71.                     var userLocation = {
  72.                         latitude: map.latitude,
  73.                         longitude: map.longitude,
  74.                         latitudeDelta: map.latitudeDelta,
  75.                         longitudeDelta: map.longitudeDelta,
  76.                         animate: true
  77.                     };
  78.                     map.mapView.setLocation(userLocation);
  79.                 }
  80.                 else {
  81.                     info("Can't get user location, lat and long is 0!");
  82.                 }
  83.             });
  84.             win.add(centerDisplay);
  85.         }
  86.  
  87.     },
  88.  
  89.     createAnnotation: function (title, subtitle, latitude, longitude, isLocation, addToMap) {
  90.  
  91.         var mapAnnotation = Ti.Map.createAnnotation({
  92.             latitude: latitude, longitude: longitude,
  93.             title: title,
  94.             subtitle: subtitle,
  95.             animate: true
  96.         });
  97.         if (isAndroid) {
  98.             mapAnnotation.pinImage = path + (isLocation ? "images/map/blue-pin.png" : "images/map/purple-pin.png");
  99.         }
  100.         else {
  101.             mapAnnotation.pincolor = isLocation ? Ti.Map.ANNOTATION_PURPLE : Ti.Map.ANNOTATION_RED;
  102.         }
  103.  
  104.         if (addToMap)
  105.             map.mapView.addAnnotation(mapAnnotation);
  106.  
  107.         return mapAnnotation;
  108.  
  109.     },
  110.  
  111.     updateAnnotation: function (mapAnnotation, title, subtitle, latitude, longitude, isLocation) {
  112.  
  113.         if (mapAnnotation) {
  114.             map.mapView.removeAnnotation(mapAnnotation);
  115.             mapAnnotation = map.createAnnotation(title, subtitle, latitude, longitude, isLocation);
  116.             map.mapView.addAnnotation(mapAnnotation);
  117.             map.mapView.selectAnnotation(mapAnnotation);
  118.         }
  119.  
  120.     },
  121.  
  122.     addAnnotation: function (mapAnnotation) {
  123.  
  124.         map.mapView.addAnnotation(mapAnnotation);
  125.  
  126.     },
  127.  
  128.     removeAnnotation: function (mapAnnotation) {
  129.  
  130.         map.mapView.removeAnnotation(mapAnnotation);
  131.  
  132.     },
  133.  
  134.     selectAnnotation: function (mapAnnotation) {
  135.  
  136.         map.mapView.selectAnnotation(mapAnnotation);
  137.  
  138.     },
  139.  
  140.     createRoute: function (name, points) {
  141.  
  142.         var route = {
  143.             name: name, points: points, color: "#7c74d4", width: 4
  144.         };
  145.         map.mapView.addRoute(route);
  146.         setTimeout(function () { map.mapView.regionFit = true; }, 700);
  147.  
  148.     },
  149.  
  150.     getLocation: function() {
  151.  
  152.         Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
  153.         Ti.Geolocation.purpose = "testing";
  154.         Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
  155.         Ti.Geolocation.distanceFilter = 10;
  156.  
  157.         if(!Ti.Geolocation.locationServicesEnabled) {
  158.             //alert('Your device has GPS turned off. Please turn it on.');
  159.             return;
  160.         }
  161.  
  162.         function updatePosition(e) {
  163.             if(!e.success || e.error) {
  164.                 info("Unable to get your location - " + e.error);
  165.                 return;
  166.             }
  167.             info(JSON.stringify(e.coords));
  168.             map.latitude = e.coords.latitude;
  169.             map.longitude = e.coords.longitude;
  170.             Ti.Geolocation.removeEventListener('location', updatePosition);
  171.         };
  172.  
  173.         Ti.Geolocation.getCurrentPosition(updatePosition);    
  174.         Ti.Geolocation.addEventListener('location', updatePosition);
  175.  
  176.     }
  177.  
  178. };