简体   繁体   English

使用Google API从邮政编码加载地图

[英]Load a map from postcode using Google API

I am using an external script to load a map and there is a button which found the exact location using the postcode. 我正在使用外部脚本加载地图,并且有一个按钮可以使用邮政编码找到确切的位置。 The question is how can I send from one function to another using Javascript? 问题是如何使用Javascript从一个函数发送到另一个函数?

The first function, where is taken the postcode is the following: 第一个函数的邮政编码如下:

function findLocation(){
    var x = document.getElementById("postcode_car_park");
    getLocation();

    function getLocation(){
        if (navigator.geolocation){
            navigator.geolocation.watchPosition(reverseGeoLookup);
        }
        else{
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function reverseGeoLookup(position) {
        console.log(position);
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        var req = new XMLHttpRequest()
        req.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=true", true)
        req.onreadystatechange = function() {
            if(req.readyState == 4) {
                var result = JSON.parse(req.response).results
                for(var i = 0, length = result.length; i < length; i++) {
                    for(var j = 0; j < result[i].address_components.length; j++) {
                        var component = result[i].address_components[j]
                        //console.log(component.long_name);
                        if(~component.types.indexOf("postal_code")) {
                            var out = document.getElementById('postcode_car_park');
                            out.style.display= "block";
                            out.innerHTML = "<b>" + component.long_name + "</b>";
                            return false;
                        }
                    }
                }
            }
        }
        req.send();

    }

}

And the other function where I want to send the component.long_name from the first one is the following: 我想从第一个发送component.long_name的另一个函数如下:

function automaticMap(){
    $("<script />", {
        "id":"scripting",
        "data-location":"http://en.parkopedia.co.uk/parking/" + postcode,
        "data-options":"l=0&tc=0&zc=1&country=UK&ts[]=4&ts[]=3&ts[]=2",
        "data-size":"650:400",
        "src":"mapView.js",
        "type":"text/javascript"
    }).appendTo("#map")
}

I know that is really easy but I have been searching in the forum and I don't know how to send variables and load the map because I have used many methods and does not work. 我知道这确实很容易,但是我一直在论坛中进行搜索,但是我不知道如何发送变量和加载地图,因为我已经使用了很多方法并且无法正常工作。 The main problem is that in the first function there are many loops and the variable is inside all of them, therefore I don't know how to access. 主要问题是在第一个函数中有很多循环,并且变量都在所有循环内部,因此我不知道如何访问。

One way would be to have reverseGeoLookup() simply return component.long_name if I understand your question correctly. 一种方法是,如果我正确理解您的问题,则使reverseGeoLookup()仅返回component.long_name Then store that value in a variable and pass it to automaticMap() as a parameter. 然后将该值存储在变量中,并将其作为参数传递给automaticMap()

So var component_long_name = reverseGeoLookup(position) and then automaticMap(component_long_name) 所以var component_long_name = reverseGeoLookup(position)然后是var component_long_name = reverseGeoLookup(position) automaticMap(component_long_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM