dongzhukuai8177 2012-10-12 06:11
浏览 61
已采纳

使用AJAX在Wordpress管理控制面板上进行地理编码地址

I am creating a website for a magazine which has a page devoted to events (such as County Shows etc.) This page has an interactive map at the top (built with google maps), and then a section with the different events and their details in a 3col grid.

My boss wanted to have it so that when the website owners (we'll refer to them as the editor) are adding the event, it is possible for them to add the address of the event. When the editor saves the event, it then geocodes the address, and saves the lat&lng into their own hidden input fields.

There is then a loop on the Events page within the Google Maps syntax to create markers for each event using their LatLng.

I have the page created with the map but I do not know how to go about geocoding the address in the back end.

I do not mind whether or not the geocoding is done automatically when saving the post, or if it is manually converted through clicking a button. I just need a way to save the LatLng so I can call it from the loop.

So how can I do this? I understand I'm going to need some jQuery for the AJAX request, but which file should this go in? (the geocode.php

Code time!

Geocode.php:

$result = json_decode(file_get_contents($geoCodeURL), true); 

echo $result['results'][0]['geometry']['location']['lat'];
echo ',';
echo $result['results'][0]['geometry']['location']['lng'];

Yeah... That's all I got.

Snippet of Functions.php

    <td>
        <form class="geocode" action="<?php bloginfo('template_url');?>/geocode.php" method="post">
        <input type="text" name="addr" />
        <input type="submit" value="" />
        </form>
        <input type="text" value="" name="eventLatLng" class="latlng" />
    </td>

P.S. I'm very sorry if this is unclear, I've never worked with AJAX and I'm a bit confused as to how to explain it, so sorry if I wasn't much help.

展开全部

  • 写回答

1条回答 默认 最新

  • douquan3294 2012-10-12 06:47
    关注

    Here is the code for enter the address by google map and get their lat & long.

     <!--code for google address api-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <input name="add" type="text" id="address" class="required" style="width: 500px;" onblur="cordinate()">
                <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>
                <script>
                    var autocomplete = new google.maps.places.Autocomplete($("#address")[0], {});
    
                    google.maps.event.addListener(autocomplete, 'place_changed', function() {
                    var place = autocomplete.getPlace();
                    console.log(place.address_components);
                    });
                </script>
                <!--code for google address api-->
    
    
    
                <!--code for google address cordinates By Harshal-->
    
                <script>
    
                function cordinate()
                {
                    geocoder = new google.maps.Geocoder(); // creating a new geocode object
    
                    address1 = document.getElementById("address").value;
    
                        if (geocoder)
                    {
                    geocoder.geocode( { 'address': address1}, function(results, status)
                    {
                    if (status == google.maps.GeocoderStatus.OK)
                    {
                    //location of first address (latitude + longitude)
                    var location1 = results[0].geometry.location;
                    document.getElementById("cor").value = location1;
    
                    //alert(location1);
                    } else
                    {
                    alert("Geocode was not successful for the following reason: " + status);
                    }
                    });
    
                    }//end of If
    
                }
                </script>
                <!--code for google address cordinates-->
                <input type="hidden" name="cor" id="cor">//in this input field you will get the cordinates.
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部