0

I have map which should show position depends on input, which I control with switch. Long and Lat I am getting from API (depends on my input) which return to me coordinates. How can I show position from my coordinates? For me all time shows same position. Here is my code: Where am I wrong?

if(smth !='' && smth!=0){

    $.ajax({
        type: "POST",
        url: 'myApiUrl',
        data: {
            'region' : data_from switch,
            'my_key': 'my_key_here'
    },
        success: function(e){
            console.log(e);
            if(e.names.length > 0){

            center = ol.proj.transform([e.names[0].X, e.names[0].Y], 'EPSG:4326', 'EPSG:3857')


            var lonlat =ol.proj.transform([e.names[0].X,e.names[0].Y], 'EPSG:4326', 'EPSG:3857'); 
            var feature =  new ol.Feature({
            geometry: new ol.geom.Point([Math.floor(lonlat[0]),Math.floor(lonlat[1])]) 
                });
                    vectorLayer.getSource().addFeature(feature);
                    map.getView().setCenter(center);
                    map.getView().setZoom(14);

                console.log(lonlat);
                loadMarkers(e.names[0].X,e.names[0].Y);
            }
        }
    });
}

Also I need to change position on change of same input, which will also use same switch and return coordinates. I have create crated function, which normally should show my position on change of input, it does not work also. Please help me to find mistake. Here is the code:

function loadMarkers(x, y){
    //console.log(x, y);
    var lonlat =ol.proj.transform([x,y], 'EPSG:4326',   'EPSG:3857'); 

    var feature =  new ol.Feature({
        geometry: new ol.geom.Point([Math.floor(lonlat[0]),Math.floor(lonlat[1])]) 
    });

    vectorLayer.getSource().addFeature(feature);
    map.getView().setCenter(lonlat, 9);
}

This function I call on input change where inserting Long as 'Y' and Lat as 'X', but it does not show me anything also, also no errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.