Добавление точек
Добрый вечер, у нас имеется код, но мне нужно что бы и точку A и точку Б можно было устанавливать. Помогите пожалуйста.
А то я целый день бьюсь не могу решить эту дилемму <!DOCTYPE html> <html> <head> <title>Примеры. Расчет стоимости доставки</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="//api-maps.yandex.ru/2.0/?load=package.full&lang=ru-RU" type="text/javascript"></script> <script type="text/javascript"> function init() { var myMap = new ymaps.Map('map', { center: [60.1, 30.1], zoom: 6, type: 'yandex#map', behaviors: ['scrollZoom', 'drag'] }), search = new ymaps.control.SearchControl({ useMapBounds: true, noCentering: true, noPlacemark: true }), calculator = new DeliveryCalculator(myMap, myMap.getCenter()); myMap.controls.add(search, { right: 5, top: 5 }); search.events.add('resultselect', function (e) { var results = search.getResultsArray(), selected = e.get('resultIndex'), point = results[selected].geometry.getCoordinates(); calculator.setStartPoint(point); }); } function DeliveryCalculator(map, finish) { this._map = map; this._start = null; this._finish = new ymaps.Placemark(finish, { iconContent: 'Б' }); this._route = null; map.events.add('click', this._onClick, this); map.geoObjects.add(this._finish); } var ptp = DeliveryCalculator.prototype; ptp._onClick= function (e) { this.setStartPoint(e.get('coordPosition')); }; ptp._onDragEnd = function (e) { var target = e.get('target'); this.setStartPoint(target.geometry.getCoordinates()); } ptp.getDirections = function () { var self = this, start = this._start.geometry.getCoordinates(), finish = this._finish.geometry.getCoordinates(); if(this._route) { this._map.geoObjects.remove(this._route); } ymaps.geocode(start, { results: 1 }) .then(function (geocode) { var address = geocode.geoObjects.get(0) && geocode.geoObjects.get(0).properties.get('balloonContentBody') || ''; ymaps.route([start, finish]) .then(function (router) { var distance = Math.round(router.getLength() / 1000), message = '<span>Расстояние: ' + distance + 'км.</span><br/>' + '<span style="font-weight: bold; font-style: italic">Стоимость доставки: %sр.</span>'; self._route = router.getPaths(); self._route.options.set({ strokeWidth: 5, strokeColor: '0000ffff', opacity: 0.5 }); self._map.geoObjects.add(self._route); self._start.properties.set('balloonContentBody', address + message.replace('%s', self.calculate(distance))); self._start.balloon.open(); }); }); }; ptp.setStartPoint = function (position) { if(this._start) { this._start.geometry.setCoordinates(position); } else { this._start = new ymaps.Placemark(position, { iconContent: 'А' }, { draggable: true }); this._start.events.add('dragend', this._onDragEnd, this); this._map.geoObjects.add(this._start); } this.getDirections(); }; ptp.calculate = function (len) { // Константы. var DELIVERY_TARIF = 20, MINIMUM_COST = 500; return Math.max(len * DELIVERY_TARIF, MINIMUM_COST); }; ymaps.ready(init); </script> </head> <body> <div id="map" style="width:900px; height:800px"></div> </body> </html> |
Часовой пояс GMT +3, время: 09:56. |