📜 ⬆️ ⬇️

Google.Maps + 1c: Enterprise 8.2

In this post I want to show an example of how Google.Maps can be used in 1c: Enterprise 8.2 configurations. As you all know, 1c has taken root (to put it mildly) in almost all commercial structures - so it did not pass us. The scope of application of cartographic systems in 1c is quite wide, for example, the possibility of overlaying reports on a map, calculating the cost of routes, and, of course, finding objects on a map by geographic coordinates, which may well be used by call center operators when consulting clients.

What am I talking about?

The narration is based on Google Maps JavaScript API V3 (all examples of working with the service are obtained from here) and of course the 1c: Enterprise 8.2 platform. The 1c components used in the example are also present in version 8.1, so it is very likely that everything described below will work there.

Learn more about Google.Maps

The 3rd version of the platform has two possibilities of use:

On both methods, Google imposes restrictions - 2500 requests for determining the coordinates per day for one ip-address. However, all this you can read in the original, the link above.
An example will be built on the first method.

And a little about 1c

We will use the usual forms mode, which works in a 1c thick client. For the new-fashioned controlled mode of working with forms, which appeared in version 8.2, this method will not work without modifications.
')
What exactly will we do:

  1. Create an html file containing javascript functions for working with the map
  2. Create an external processing for 1c that displays a map and has controls that allow you to display labels found by coordinates or address


So, to the point:

1. Create html file

To work with the service, we need a file that will contain the javascript functions we need and will open from 1c. I will not give his example, because they have done it for me here: Hello, world! , but go to the description.
Add functions that, when opening an html document, get the coordinates of the “Moscow” point and initialize the map with automatic positioning on it:
var map = null;
function initialize()
{
getLocation('', function (results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
GoogleLocation = results[0].geometry.location;
initializeMap(GoogleLocation);
addMarkerForLocation(GoogleLocation.lat(), GoogleLocation.lng());//
addMarkerForAddress(' ');//
} else alert(' ');
});
}

//GoogleLocation - ,
function initializeMap(GoogleLocation)
{
if (GoogleLocation != null)
{
var latlng = new google.maps.LatLng(GoogleLocation.lat(), GoogleLocation.lng());
var myOptions = {
zoom: 10,//
center: latlng,//
mapTypeId: google.maps.MapTypeId.ROADMAP//
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
} else alert(' ');
}



A function that returns geographic coordinates at:

//address - , ', '
//checkResult -
function getLocation(address, checkResult)
{
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address }, checkResult);
}



Functions that add a label to the map:
//
function addMarkerForLocation(lat, lng)
{
if ((lat != null)&&(lng!=null))
{
var marker = new google.maps.Marker(
{
position: new google.maps.LatLng(lat, lng),
map: map,
title: ' , ',
icon: 'http://google-maps-icons.googlecode.com/files/factory.png'
});
var infoWindow = new google.maps.InfoWindow;
bindInfoWindow(marker, infoWindow, ' html');
} else alert(' ');
}

// ,
function addMarkerForAddress(address)
{
if (map != null)
{
getLocation(address, function (results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var marker = new google.maps.Marker(
{
position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()),
map: map,
title: address,// ,
icon: 'http://google-maps-icons.googlecode.com/files/factory.png'//
});
} else alert(' ');
});
} else alert(' ');
}



And of course a function that can display the label description in the information window:

//marker - ,
//infowindow - google.maps.InfoWindow
//html - html,
function bindInfoWindow(marker, infoWindow, html)
{
if (map != null)
{
google.maps.event.addListener(marker, 'click', function ()
{
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
} else alert(' ');
}

2. We create processing in 1s

On the 1c processing form, we add a control of the FieldHTMLDocument type (let's call it “FieldHTMLDoc”) - it will display the html file we created earlier (let's call it google.html). In practice, it is also possible to use the Microsoft Web Browser ActiveX object, but this is to the taste, especially considering that IE will be used in any case.
After we added the HTML document field, we need to make sure that the map opens automatically when the processing is opened. To do this, add the “Opening” form handler, and add to it: Elements of the Form. FieldHTMLDoc.Switch (“c: \ google \ google.html”);
It is enough that when starting our external processing in 1c, our html-file opens automatically, in which, when the page loads, the Initialize () function starts, which fills and centers the map.
If everything is ok, let's add the ability to add tags for the event in 1c:
To do this, add to the form a text field for entering “Address”, “Latitude”, “Longitude” and two buttons - “Add label at address” and “Add label by coordinates”.
And of course handlers corresponding to the buttons:
.HTML..parentWindow.eval("addMarkerForAddress('" + + "')");
.HTML..parentWindow.eval("addMarkerForLocation(" + (, "=") + "," + (, "=") + ")");

As you already understood, both handlers call the Javascript functions when clicking buttons, which were set by us in the html-file and create marks on the map. Actually, for the first version of this is already more than enough.

Undoubtedly, in practice this method can be improved - for example by the fact that in 1c it is possible to do without the html file at all using the FieldHTMLDocument object method. InstallText () - while the code of the page itself can be stored in the 1c database as text and not bother with external html file.
It is also worth thinking about the fact that Google limits us in the number of requests for determination of coordinates - 2500 requests per day from one ip. It would be logical to add storage in the database of the most used coordinates, which, in addition to saving queries, will allow additional functionality in the form, for example, of the ability to select and display the desired labels within the specified geographical coordinates relative to a given center.

That's all. I hope my first post on Habré was useful for you!

Source: https://habr.com/ru/post/139444/


All Articles