﻿var geocoder = new GClientGeocoder();
geocoder.setBaseCountryCode("hu");
var addressMarker; //Marker to store the last location.

//geocoding
function showAddress(address)
{
//hack, refactor when the editor and the viewer are united
	if ((typeof (map) == "undefined")||(null == map))
		map = MyGMaps.FindMap("").gMap;

	if (geocoder != null)
	{
		geocoder.getLocations(address, function(resp) { addAddressToMap(resp, map); });
	}
}

function addAddressToMap(response, gmap)
{
	if (addressMarker != null)
		gmap.removeOverlay(addressMarker);
	if (!response || response.Status.code != 200)
		TrackHelpers.Alert("Sikertelen geokódolás...");
	else
	{
		place = response.Placemark[0];
		addressMarker = addMarkerToMap(
        place.Point.coordinates[1],
        place.Point.coordinates[0],
        gmap.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(place.ExtendedData.LatLonBox.south, place.ExtendedData.LatLonBox.west), new GLatLng(place.ExtendedData.LatLonBox.north, place.ExtendedData.LatLonBox.east))),
        gmap);
		addressMarker.openInfoWindowHtml(place.address); // + "<br>" +"<b>Country code:</b> " + place.AddressDetails.Country.CountryNameCode);
	}
}

function addMarkerToMap(lat, lon, zoomfactor, gmap, icon)
{
	var point = new GLatLng(lat, lon);
	var myMarker;

	if ((icon) && (typeof icon != "undefined"))
		myMarker = new GMarker(point, icon);
	else
		myMarker = new GMarker(point);

	gmap.addOverlay(myMarker);

	if ((zoomfactor) && (typeof zoomfactor != "undefined"))
		gmap.setCenter(point, zoomfactor);

	return myMarker;
}

