如何获取函数返回[重复]

时间:2022-09-10 23:18:53

This question already has an answer here:

这个问题在这里已有答案:

How can I return the name of the city to show on html page?

如何在html页面上返回要显示的城市名称?

My function

function getCity(latitude, longitude){

var geocoder;
geocoder = new google.maps.Geocoder(); 
var latlng = new google.maps.LatLng(latitude, longitude);
geocoder.geocode(
    {'latLng': latlng}, 
    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var add= results[0].formatted_address ;
                    var  value=add.split(",");

                    count=value.length;
                    country=value[count-1];
                    state=value[count-2];
                    city=value[count-3];
                    alert("city name is: " + city);
                    return city;
                }
        }

    }
);

The alert is working fine.

警报工作正常。

In HTML I called this function like that:

在HTML中我称之为这样的函数:

 <script> getCity("<%= map.latitude %>","<%= map.longitude %>")</script>

Tks in advance.

事先提前。

3 个解决方案

#1


0  

Change your code to:

将您的代码更改为:

<script> document.write(getCity("<%= map.latitude %>","<%= map.longitude %>"))</script>

It will write into the document what your Javascript function returns.

它会在文档中写入您的Javascript函数返回的内容。

#2


0  

Create a DIV in your HTML, like this:

在HTML中创建DIV,如下所示:

<div id="cityName"></div>

And in the javascript, replace the "alert" with this code:

在javascript中,将“alert”替换为以下代码:

document.getElementById("cityName").innerHTML(city);

#3


0  

If the alert works fine that means returned value is true. Then u can just call the function that returns the city name anywhere in the document body. Alternatively since the function only returns a string, u just need to assign it to a new variable and add more information to the returned function city string.

如果警报工作正常,则表示返回值为true。然后,您只需调用在文档正文中的任何位置返回城市名称的函数。或者,因为函数只返回一个字符串,所以你只需要将它分配给一个新变量,并将更多信息添加到返回的函数city字符串中。

Like**function city () Return a city name value.

像** function city()返回城市名称值。

then var cityname = city() + 'more information';

那么var cityname = city()+'more information';

**

#1


0  

Change your code to:

将您的代码更改为:

<script> document.write(getCity("<%= map.latitude %>","<%= map.longitude %>"))</script>

It will write into the document what your Javascript function returns.

它会在文档中写入您的Javascript函数返回的内容。

#2


0  

Create a DIV in your HTML, like this:

在HTML中创建DIV,如下所示:

<div id="cityName"></div>

And in the javascript, replace the "alert" with this code:

在javascript中,将“alert”替换为以下代码:

document.getElementById("cityName").innerHTML(city);

#3


0  

If the alert works fine that means returned value is true. Then u can just call the function that returns the city name anywhere in the document body. Alternatively since the function only returns a string, u just need to assign it to a new variable and add more information to the returned function city string.

如果警报工作正常,则表示返回值为true。然后,您只需调用在文档正文中的任何位置返回城市名称的函数。或者,因为函数只返回一个字符串,所以你只需要将它分配给一个新变量,并将更多信息添加到返回的函数city字符串中。

Like**function city () Return a city name value.

像** function city()返回城市名称值。

then var cityname = city() + 'more information';

那么var cityname = city()+'more information';

**