Answer the question
In order to leave comments, you need to log in
Can't translate coordinates to Yandex map address?
There is a code
ymaps.modules.define(
'DeliveryCalculator',
['util.defineClass'],
function (provide, defineClass) {
/**
* @class DeliveryCalculator Calculate shipping cost.
* @param {Object} map Map instance.
*/
function DeliveryCalculator (map) {
this._map = map;
this._startPoint = null;
this._route = null;
this._startPointBalloonContent;
this._finishPointBalloonContent;
map.events.add('click', this._onClick, this);
}
defineClass( DeliveryCalculator, {
/**
* Create the starting point of the route.
* If the point is created, then update the coordinates.
* @param {Number[]} position Point coordinates.
*/
setStartPoint: function (position) {
console.log('change start');
if (this._startPoint) {
this._startPoint.geometry.setCoordinates(position);
} else {
// Create a draggable marker (option `draggable`).
// When the drag is complete, call the `_onStartDragEnd` handler.
this._startPoint = new ymaps.Placemark(position, {iconContent: 'A'}, {draggable: true});
this._startPoint.events.add('dragend', this._onStartDragEnd, this);
this._map.geoObjects.add(this._startPoint);
}
this.geocode('start', position);
},
/**
* Create the end point of the route.
* If the point is created, then update the coordinates.
* @param {Number[]} position Point coordinates.
*/
setFinishPoint: function (position) {
console.log('finish');
if(this.
this._finishPoint.geometry.setCoordinates(position);
} else {
this._finishPoint = new ymaps.Placemark(position, { iconContent: 'B' }, { draggable: true });
this._finishPoint.events.add('dragend', this._onFinishDragEnd, this);
this._map.geoObjects.add(this._finishPoint);
}
if (this._startPoint) {
this.geocode('finish', position);
}
},
/**
* Perform reverse geocoding (determine address by coordinates) for route points.
* @param {String} pointType Point type: 'start' - start, 'finish' - end.
* @param {Number[]} point Point coordinates.
*/
geocode: function (pointType, point) {
ymaps.geocode(point).then(function (result) {
// result contains the description of the found geo objects.
if (pointType == 'start') {
// Get the description of the first geo object in list to then show
// with a description of click delivery on the label
this._startPointBalloonContent = result.geoObjects.get(0) &&
result.geoObjects.get(0).properties.get('balloonContentBody') || '';
} else {
// Same for endpoint
this._finishPointBalloonContent = result.geoObjects.get(0) &&
result.geoObjects.get(0).properties.get('balloonContentBody') || '';
}
this._setupRoute();
}, this);
},
/**
*
* @param {Number} routeLength Route length in kilometers.
* return {Number} Shipping cost.
calculate: function (routeLength) {
// Constants.
var DELIVERY_TARIF = 0, // Cost per kilometer.
MINIMUM_COST = 0; // Minimum cost.
return Math.max(routeLength * DELIVERY_TARIF + MINIMUM_COST);
},
/**
* Paving a route through the given points
* and calculate the delivery.
*/
_setupRoute: function () {
// Remove the previous route from the map.
if (this._route) {
this._map.geoObjects.remove(this._route);
}
if (this._startPoint && this._finishPoint) {
var start = this._startPoint.geometry.getCoordinates(),
finish = this._finishPoint.geometry.getCoordinates(),
startBalloon = this._startPointBalloonContent,
finishBalloon = this._finishPointBalloonContent;
// Paving a route through the given points.
ymaps.route([start, finish])
.then(function (router) {
var distance = Math.round(router.getLength() / 1000),
message = 'Distance: ' + distance + ' km.
' +
'SELECT VEHICLE TYPE ----------->>>';
this._route = router.getPaths(); // Get the collection of paths that make up the route.
this._route.options.set({ strokeWidth: 5, strokeColor: '0000ffff', opacity: 0.5 });
this._map.geoObjects.add(this._route); // Add a route to the map.
// Set the content of the balloon for the start and end marker.
this._startPoint.properties.set('balloonContentBody', startBalloon + message.replace('%s', this.calculate(distance)));
this._finishPoint.properties.set('balloonContentBody', finishBalloon + message.replace('%s', this.calculate(distance)));
// $('#ss').html(start);
//$('#ff').html(finish);
// Open the balloon above the delivery point.
// Comment out if you don't want the balloon to show automatically.
this._finishPoint.balloon.open();
}, this);
this._map.setBounds(this._map.geoObjects.getBounds());
}
},
/**
* Map click handler. We get the coordinates of a point on the map and create a marker.
* @param {Object} event The event.
*/
_onClick: function (event) {
if (this._startPoint) {
this.setFinishPoint(event.get('coords'));
} else {
this.setStartPoint(event.get('coords'));
}
},
/**
* Get the marker coordinates and call the geocoder for the starting point.
*/
_onStartDragEnd: function () {
this.geocode('start', this._startPoint.geometry.getCoordinates());
},
_onFinishDragEnd: function () {
this.geocode('finish', this._finishPoint.geometry.getCoordinates());
}
});
provide(DeliveryCalculator);
}
);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question