doupiai5597 2015-08-10 11:14
浏览 55

邮政编码搜索并返回一个链接

thanks for your help in advance.

I have a wordpress site running woocommerence and I have it set up as a multi site.

Basically I run a business which delivers McDonald's and KFC to customers. Each area has it's own site which is part of the multisite i've set up.

The main site which functions to give the user the link to the multisite which delivers in their area.

I need to run a simple code on the main site so when the user types in their postcode or village then it takes them to the right part of the multisite for them.

for example www.foodtoyourdoor.co.uk is the main site and the user would type in their postcode or say village "Durham" when the user hits enter it takes them directly to durham.foodtoyourdoor.co.uk.

i've been looking at yourtube for weeks and cant find anything to help me.

  • 写回答

1条回答 默认 最新

  • douzhuang1900 2015-08-10 12:01
    关注

    The first step you need to do is store the longitude and latitude of the sites in the database. When you've done that you can convert the posted address of a client to longitude and latitude as well:

    function getLongitudeAndLatitude($address) {
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&language=nl&address=' . str_replace(' ','+', $address);
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        curl_close($ch);  
        $obj = simplexml_load_string($response);
    
        $lat = null;
        $lon = null;
    
        if ($obj) {
            $obj = @$obj->result;
            $lat = (@$obj->geometry->location->lat.'');
            $lon = (@$obj->geometry->location->lng.'');
        }
        return array( 'latitude' => $lat, 'longitude' => $lon,);
    }
    

    With this data you then can calculate the distance between the client and the sites. To do this you can create a routine in mysql

    CREATE DEFINER=`user`@`localhost` FUNCTION `haversine`(`lat1` FLOAT, `lon1` FLOAT, `lat2` FLOAT, `lon2` FLOAT) RETURNS float
        NO SQL
        DETERMINISTIC
        COMMENT 'Returns the distance in degrees on the Earth             between two known points of latitude and longitude'
    BEGIN
        RETURN 111.045*DEGREES(ACOS(
                  COS(RADIANS(lat1)) *
                  COS(RADIANS(lat2)) *
                  COS(RADIANS(lon2) - RADIANS(lon1)) +
                  SIN(RADIANS(lat1)) * SIN(RADIANS(lat2))
                ));
    END
    

    With this routine you then can create a SQL that returns the nearest site

    $user_location = getLongitudeAndLatitude($_POST['txt_address']);
    if (isset($user_location['latitude']) && isset($user_location['longitude'])) {
        $stmt = $pdo->prepare('SELECT *, haversine(:lat1, :lon1, site.latitude, site.longitude) as distance FROM site ORDER BY distance LIMIT 1');
        $stmt->execute([':lat1' => $user_location['latitude'], ':lon1' => $user_location['longitude']);
        $res = $stmt->fetch(PDO::FETCH_ASSOC);
        var_dump($res);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?