Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将地址/地理编码转换为Lat-Long坐标-谷歌地图的蜘蛛侠_Php_Javascript_Mysql_Google Maps_Google Maps Api 3 - Fatal编程技术网

Php 将地址/地理编码转换为Lat-Long坐标-谷歌地图的蜘蛛侠

Php 将地址/地理编码转换为Lat-Long坐标-谷歌地图的蜘蛛侠,php,javascript,mysql,google-maps,google-maps-api-3,Php,Javascript,Mysql,Google Maps,Google Maps Api 3,我试图在代码中实现spiderfier,但在将字符串地址转换为(lat-lon)坐标时遇到了一些困难 演示: 文件: 这是我从数据库中获取地址的方式,我现在需要将其转换为(Lat,Lon)格式 <?php $locations = new locations; $userid = '1'; $cityArray = $locations->pastLocations($userid); $title = $locations->pastTitles($user

我试图在代码中实现spiderfier,但在将字符串地址转换为(lat-lon)坐标时遇到了一些困难

演示:

文件:

这是我从数据库中获取地址的方式,我现在需要将其转换为(Lat,Lon)格式

<?php 
  $locations = new locations;
  $userid = '1';
  $cityArray = $locations->pastLocations($userid);
  $title = $locations->pastTitles($userid);
  $length = count($cityArray);
?>

//now that I have the cityArray acquired from php, I encode them for javascript
<script>
var cityArray= <?php echo json_encode($cityArray); ?>;
var title = <?php echo json_encode($title); ?>;

//现在我已经从php获得了cityArray,我将它们编码为javascript
var cityArray=;
var title=;
好的,现在我有了一个数组中的地址。spiderfier的示例使用以下命令生成随机位置(足够接近以重叠

var baseJitter = 2.5;
var clusterJitterMax = 0.1;
var rnd = Math.random;
var data = [];
var clusterSizes = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,2, 3, 3, 4, 5, 6, 7, 8, 9, 12, 18, 24];
for (var i = 0; i < clusterSizes.length; i++) {
GClientGeocoder.getLatLng()
var baseLon = -1 - baseJitter / 2 + rnd() * baseJitter;
var baseLat = 52 - baseJitter / 2 + rnd() * baseJitter;
var clusterJitter = clusterJitterMax * rnd();
for (var j = 0; j < clusterSizes[i]; j ++) data.push({
  lon: baseLon - clusterJitter + rnd() * clusterJitter,
  lat: baseLat - clusterJitter + rnd() * clusterJitter,
  h:   new Date(1E12 + rnd() * 1E11).toString(), //this is the title
  d:   Math.round(rnd() * 100) + '% happy'
});
} 
window.mapData = data; 
var baseJitter=2.5;
var clusterJitterMax=0.1;
var rnd=数学随机;
var数据=[];
var clusterSizes=[1,1,1,1,2,2,2,2,2,3,3,4,5,6,7,8,9,12,18,24];
对于(变量i=0;i
以下是spiderfier用于绘制数据数组的代码。这就是我在生成它使用的数据数组时遇到的问题

var bounds = new gm.LatLngBounds();

  for (var i = 0; i < window.mapData.length; i ++) {
    var datum = window.mapData[i];
    var loc = new gm.LatLng(datum.lat, datum.lon);
    bounds.extend(loc);
    var marker = new gm.Marker({
      position: loc,
      title: datum.h,
      map: map,
      icon: iconWithColor(usualColor),
      shadow: shadow
    });
    marker.desc = datum.d;
    oms.addMarker(marker);
  }
  map.fitBounds(bounds);
var bounds=new gm.LatLngBounds();
对于(var i=0;i
假设我有一个可以输入地理代码的数组,我如何将地址(例如德克萨斯州达拉斯)转换成可以与spiderfier一起使用的(lat-lon)格式?请记住,这个数组中有不止一个地址,因此我必须循环并以某种方式将每个地址推送到数据数组中。这就是我最困惑的地方

谢谢

编辑-我尝试编写一些代码,循环遍历我的cityArray和地理代码,然后将其插入一个名为data的数组中,但不幸的是,它不起作用

这是密码。当我使用document.write(数据)时,不显示任何内容。我也没有收到任何错误,所以我不确定我做错了什么

var data = [];
for (var i = 0; i < cityArray.length; i++) {
    //document.write(cityArray[i]);
    geocoder.geocode( { 'address': cityArray[i] }, function(results, status) { //use latlang to enter city instead of coordinates 
        if (status == google.maps.GeocoderStatus.OK) {
            data.push({
                position:results[0].geometry.location,
                h: 'test',
                d: 'test'
            });
          }
          else {
            alert("Geocode was not successful for the following reason: " + status);
            }   
        });  
}  
var数据=[];
对于(var i=0;i
您的问题是如何将地址转换为地理坐标(纬度和纬度),以便与谷歌地图一起使用

将地址转换为坐标的过程称为地理编码

Google Maps API v3包含一个用于用户输入地址的API

还有一个供服务器使用的

两者都受到配额和税率限制

请参阅此部分,了解它们的使用说明以及它们何时适用的讨论


如果您有一个数据库,最理想的选择是在数据库中输入地址时对其进行地理编码,然后使用数据库中的坐标来放置标记,而不是“动态”地对其进行地理编码。

我编辑了我的帖子,以显示我在地理编码过程中所做的更改。介意看一看吗?document.write在呈现页面上使用时,会覆盖现有页面,这不是调试的好方法。请参阅Andrew Leach的答案,了解如果您必须“动态”进行地理编码,如何限制地理编码程序以获得结果(如果您有10个以上的标记,则会降低页面负载),以及为什么您的地理编码调用不起作用,你能提供一个链接或一个JSFIDLE来说明问题吗?我也看到了地理编码函数的编码方式存在问题,地理编码程序异步返回结果,因此输入顺序和输出顺序可能不同,看起来你在假设它们是一样的。我试图让它在JSFIDLE中工作,但什么也看不出来。