Google地图 - 将地址转换为纬度&经度 - PHP后端? [英] Google Maps - converting address to latitude & longitude - PHP backend?

查看:115
本文介绍了Google地图 - 将地址转换为纬度&经度 - PHP后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以转换(在PHP后端):

 <?php 
$地址=街道1,城市,国家; //只是普通地址
?>

 <?php 
$ LatLng =10.0,20.0; //纬度& lonitude
?>

我目前使用的代码是默认的代码:

 < script type ='text / javascript'src ='https://maps.googleapis.com/maps/api/js?v = 3.exp& #038;传感器=假'>< /脚本> 

< script>

函数initialize(){
var myLatlng = new google.maps.LatLng(10.0,20.0);
var mapOptions = {
zoom:16,
center:myLatlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'),mapOptions);

var marker = new google.maps.Marker({
position:myLatlng,
map:map,
title:'This is a caption'
});
}

google.maps.event.addDomListener(window,'load',initialize);

< / script>

< div id =map>< / div>

我一直在阅读https://developers.google.com/maps/documentation/geocoding 了一段时间,但我没有足够的经验。



Thanks。

解决方案

这适用于我:

 <?php 
$地址= urlencode($地址);
$ request_url =http://maps.googleapis.com/maps/api/geocode/xml?address=\".$Address.\"&sensor=true;
$ xml = simplexml_load_file($ request_url)或die(url not loading);
$ status = $ xml->状态;
if($ status ==OK){
$ Lat = $ xml-> result->> geometry-> location-> lat;
$ Lon = $ xml-> result-> geometry-> location-> lng;
$ LatLng =$ Lat,$ Lon;
}
?>

参考文献:


is it possible to convert (in PHP back-end):

<?php
  $Address = "Street 1, City, Country"; // just normal address
?>

to

<?php
  $LatLng = "10.0,20.0"; // latitude & lonitude
?>

The code I'm currently using is the default one:

<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?v=3.exp&#038;sensor=false'></script>

<script>

function initialize() {
  var myLatlng = new google.maps.LatLng(10.0,20.0);
  var mapOptions = {
    zoom: 16,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById('map'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'This is a caption'
  });
}

google.maps.event.addDomListener(window, 'load', initialize); 

</script>

<div id="map"></div>

I've been reading https://developers.google.com/maps/documentation/geocoding for a while now but I'm not experienced enough I guess.

Thanks.

解决方案

This works for me:

<?php
  $Address = urlencode($Address);
  $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
  $xml = simplexml_load_file($request_url) or die("url not loading");
  $status = $xml->status;
  if ($status=="OK") {
      $Lat = $xml->result->geometry->location->lat;
      $Lon = $xml->result->geometry->location->lng;
      $LatLng = "$Lat,$Lon";
  }
?>

References:

这篇关于Google地图 - 将地址转换为纬度&amp;经度 - PHP后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆