douxun7992 2017-01-04 02:05
浏览 36
已采纳

使用PHP编码Google Maps地理编码器的变量

I am trying to encode variables for the URL for the google maps geocoding API with PHP. I have used both urlencode and rawurlencode and I get the same result for

Székesfehérvár, Hungary

PHP returns

Sz%E9kesfeh%E9rv%E1r%2C%20Hungary

Which cannot be decoded by any of the online decoders.

When I encode with the online encoder tools I get:

Sz%C3%A9kesfeh%C3%A9rv%C3%A1r%2C+Hungary

When I plug that in directly to the Google Maps geocoder URL, it is successful. The PHP function fails every time.

This is the code I am using:

function getLatandLong($landmark,$city,$state)
{

global $lat;
global $long;
global $city;
if (!empty($landmark)) {  $encoded_vars = "$landmark,$city,+$state";
} else { $encoded_vars = "$city, $state"; }

$encodedVars = urlencode($encoded_vars);
$doc = new DOMDocument();

$doc->load("http://maps.google.com/maps/api/geocode/xml?address=$encodedVars&sensor=false"); //input address
  • 写回答

1条回答 默认 最新

  • doulian7305 2017-01-04 02:35
    关注

    Well, I tried

    $encodedVars = utf8_encode($encoded_vars);
    

    And that seems to be working, even though the echo shows (probably because I don;t have the right page encoding set on my catch page:

    Székesfehérvár,+Hungary
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染