2010-11-19 74 views
1

我有我自己的PHP脚本,如:获取Google Maps URL? :)

<?php 

    <iframe width="425" height="350" frameborder="0" scrolling="no" src="' .$url .'"></iframe> 

?> 

正如你可以看到它的默认谷歌地图的iframe(访问maps.google.com,键入任何内容,然后点击“链接”右侧顶部和检查代码udner“粘贴HTML以嵌入网站”,看看它是什么样子)。

重点是我想为$ url变量创建用户友好的输入。

此时用户必须转到Google网站,从“链接”中复制第二个网址,然后删除其他所有内容并仅在输入中输入src = url。

有没有办法在我的网站上创建Google地图搜索,搜索后会用位置数据更新我的输入?

应该看起来像:

http://jsfiddle.net/rMLKJ/

回答

0

您可以使用谷歌的地理编码Web服务。

http://code.google.com/intl/en/apis/maps/documentation/geocoding/index.html

这是一个HTTP请求:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false 

如果你想这样做服务器端。你从你的php代码发出请求,然后你将获得你的坐标,并且你可以将它们传递给前端。

如果您想要客户端方法,您可以通过ajax从JavaScript发出请求,然后使用另一个Ajax调用将坐标发送到服务器。

1

我已经为你设置here

一个简单的PHP脚本中使用谷歌的地理编码API

<?php 

$address = $_GET['address']; 
$address=str_replace(" ","+",$address); 
if ($address) { 
    $json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address. 
    '&sensor=true'); 
    echo $json; 
} 

?> 

我已经使用jQuery的这里,使Ajax请求获得坐标演示。经度和纬度由PHP脚本返回的JSON解析,并创建一个标记以显示地图上的位置。

function getMarker() { 
    var address=$('#address').val(); 
    address=address.replace(/ /g,"+"); 

    $.getJSON("getjson.php?address="+address, 
     function(data){ 
      lat=data.results[0].geometry.location.lat; 
      lng=data.results[0].geometry.location.lng; 
      point= new google.maps.LatLng(lat,lng); 
      map.setCenter(point); 
      zoom = 14; 
      marker = createMarker(point,"<h3>Marker"+(markersArray.length+1)+"</h3>",zoom); 

      var newLi=document.createElement("li"); 
      var newA=document.createElement("a"); 
      var newText = document.createTextNode("Marker "+(markersArray.length+1)); 
      newA.appendChild(newText); 
      newLi.appendChild(newA); 
      newA.setAttribute('onclick','displayMarker(this.innerHTML);'); 
      document.getElementById('marker_tabs').appendChild(newLi); 
      markersArray.push(marker); 
     } 
    ); 

} 

根据用户输入的搜索查询,Google可能会返回多个结果。由于这只是一个例子,我已经拿到了第一个结果。

0

它非常简单:

<?php $url = str_replace('&', '&amp;', $url).'&amp;output=embed'; ?> 

<iframe width="425" height="350" frameborder="0" scrolling="no" src="<?php echo $url ?>"></iframe> 
-1

退房,你可以使用Google Maps URLs 这是现在的地图API产品的一部分建立此类URL的新途径。

+0

虽然此链接可能会回答问题,但最好在此处包含答案的重要部分并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/16222564) – 2017-05-24 18:07:18

+0

ack。我添加了全名和链接。干杯。 – kaskader 2017-05-24 18:57:10