//crea el mapa
function crearMapa(idDivMapa){
    if(!idDivMapa)
        idDivMapa = "mapaGM";
    var objMapa = $(idDivMapa);
    if(objMapa){
        if(GBrowserIsCompatible()){
            var mapa = new GMap2(objMapa);
            //asignamos todos los metodos personalizados
            inicializarMapa(mapa);
            //Aņade la capacidad de aumentar el zoom con doble click
            mapa.enableDoubleClickZoom();
            //ponemos el control de mapa
            mapa.addControl(new GOverviewMapControl(new GSize(100,100)));
            mapa.addControl(new GLargeMapControl());
            mapa.enableContinuousZoom();
            mapa.addControl(new GMapTypeControl());
            //ponemos una posicion por defecto
            mapa.ponerCentro();
            mapa.ponerTipo();

            //popup de los nombres de recurso
            popupMarcador = document.createElement("div");
            mapa.getContainer().appendChild(popupMarcador);
            popupMarcador.style.visibility="hidden";
            popupMarcador.className = "popupMarcadorMapa";

            return mapa;
        }else
            alert("Este navegador no tiene soporte para mapas");
    }else
        alert("No se ha creado un mapa");
}

//Geolocaliza la direccion pasada y llama a la funcion de retorno Ok pasandole el punto en un GPoint
function geolocalizar(direccion,funcionRetornoOk,funcionRetornoError,soloEnClm){
    var geo = new GClientGeocoder();
    if(soloEnClm)
        geo.setViewport(limitesCLM());
    geo.getLatLng(
                    direccion,
                    function(point){
                        if(!point)
                            funcionRetornoError();
                        else
                            funcionRetornoOk(point);
                    }
    );
}

//Geolocaliza la direccion pasada y llama a la funcion de retorno Ok pasandole la direccion
function geolocalizarR(latitud,longitud,funcionRetornoOk,funcionRetornoError){
    var geo = new GClientGeocoder();
    geo.setViewport(limitesCLM());
    geo.getLocations(
                        new GLatLng(latitud,longitud),
                        function(respuesta){
                            if(!respuesta || respuesta.Status.code!=200)
                                funcionRetornoError();
                            else
                                funcionRetornoOk(respuesta.Placemark[0].address.replace(/, Espaņa/,""));
                        }
    );
}

//ejecuta sobre el mapa el como llegar desde un punto a otro
function comoLlegar(origen, destino, funcionOk, funcionError,obtenerPasos){
    var gDir = new GDirections();
    GEvent.addListener(gDir,"load",function(){ funcionOk(gDir); });
    GEvent.addListener(gDir,"error",funcionError);
    gDir.load("from: "+origen+" to: "+destino,{ "locale": "es","getPolyline":true,"getSteps":(obtenerPasos?true:false)});
}

//ejecuta sobre el mapa el como llegar con N puntos
function comoLlegarWaypoints(puntos,funcionOk,funcionError,obtenerPasos){
    if(puntos.length>=2 && puntos.length<=25){
        var gDir = new GDirections();
        GEvent.addListener(gDir,"load",function(){ funcionOk(gDir); });
        GEvent.addListener(gDir,"error",funcionError);
        gDir.loadFromWaypoints(puntos,{"getPolyline":true,"getSteps":(obtenerPasos?true:false)});
    }
}