searchadress.js 1.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * Used for the search address feature
 */

//load address
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {

        var coordinate = JSON.parse(request.responseText);
        var lat = coordinate.results[0].geometry.lat;
        var lng = coordinate.results[0].geometry.lng;
        console.log(lat + "  " + lng);
        fly(lat, lng);
    }
}

//sending a request to the opencage api to convert the input to coordinates
function searchaddress() {
    var query = document.getElementById('SearchAddress').value;
    request.open("GET", 'https://api.opencagedata.com/geocode/v1/json?key=c73da14969e6408ab4535b3ad6dc43ea&pretty=1&no_annotations=1&q=' + query, true);
    request.send();
}

//used to fly to the found position
function fly(lat, lng) {
    var pinBuilder = new Cesium.PinBuilder();

    var bluePin = viewer.entities.add({
        name: "Blank blue pin",
        position: Cesium.Cartesian3.fromDegrees(lng, lat),
        billboard: {
            image: pinBuilder.fromColor(Cesium.Color.ROYALBLUE, 48).toDataURL(),
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        },
    });
    viewer.zoomTo(bluePin);

    viewer.flyTo(bluePin);
}