Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  var init = {
  2.  
  3.  
  4.  
  5.         makeMap: function () {
  6.             var loc = this.location.split(",");
  7.             var pos = new google.maps.LatLng(loc[0],loc[1]);
  8.  
  9.             var mapOptions = {
  10.                 zoom: 16,
  11.                 center: pos,
  12.                 mapTypeId: google.maps.MapTypeId.ROADMAP
  13.             };
  14.  
  15.             this.mapObj = new google.maps.Map(document.querySelector("#map"),mapOptions);
  16.             this.destination = pos;
  17.  
  18.             var marker = new google.maps.Marker({
  19.                 map: this.mapObj,
  20.                 position: pos,
  21.                 animation: google.maps.Animation.BOUNCE,
  22.                 icon: this.options.mapMarker
  23.             });
  24.         },
  25.  
  26.  
  27.         handleRoute: function (result,status) {
  28.  
  29.             if(status != google.maps.DirectionStatus.OK || !result.routes[0]){
  30.                 alert("Wprowadziłeś złe dane!");
  31.                 return false;
  32.             }
  33.  
  34.             this.pathRender.setDirections(result);
  35.             this.fromInput.value = result.routes[0].legs[0].start_adress;
  36.         },
  37.  
  38.  
  39.         prepareRoute: function(){
  40.             var renderOption = {
  41.                 map: this.mapObj,
  42.                 polylineOptions: {
  43.                     strokeColor:"#ff0000",
  44.                     strokeWeight:4,
  45.                     strokeOpacity:0.8
  46.                 },
  47.                 suppressMarkers: true
  48.             };
  49.            
  50.             this.pathRender.setOptions(renderOption);
  51.  
  52.  
  53.             var pathData = {
  54.                 origin: this.formInput.value,
  55.                 destination: this.destination,
  56.                 travelMode: google.maps.DirectionsTravelMode.DRIVING
  57.             };
  58.  
  59.             this.path.route(pathData, this.handleRoute.bind(this))
  60.         },
  61.  
  62.         map: function (options) {
  63.  
  64.             if(!options.location) return;
  65.  
  66.             try{ google.maps.event.addDomListener(window,"load", this.makeMap.bind(this)); } catch(e) {return;}
  67.             this.options = options;
  68.             this.location = this.options.location;
  69.             this.form = document.querySelector("#mapForm");
  70.  
  71.             this.formInput = document.querySelector("#form");
  72.             this.path = new google.maps.DirectionsService();
  73.             this.pathRender = new google.maps.DirectionsService();
  74.  
  75.             this.form.onsubmit = function(e){
  76.                 e.preventDefault();
  77.                 this.prepareRoute();
  78.             }.bind(this);
  79.  
  80.  
  81.         }
  82.  
  83.  
  84.     };
  85. init.map({
  86.         location: "52.16235,21.071409",
  87.         mapMarker:"./map_marker.png"
  88.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement