试图从mySQL数据库中包含的城市创建一系列纬度和经度

[英]Trying to create an array of latitudes and longitudes from cities contained in a mySQL database


I am trying to use Google Maps API to create an array of latitudes and longitudes (from a mySQL database of cities) that I can use as markers.

我正在尝试使用Google Maps API创建一系列纬度和经度(来自mySQL的城市数据库),我可以将其用作标记。

I am unsure of how to dynamically pull the cities in PHP and geocode them in javascript.

我不确定如何在PHP中动态拉动城市并在javascript中对它们进行地理编码。

This is what I have so far. I know the geocoder line is incorrect, but I am not sure how to work it in. Any advice or solutions would be very helpful.

这就是我到目前为止所拥有的。我知道地理编码器线路不正确,但我不知道如何使用它。任何建议或解决方案都会非常有用。

  var geocoder = google.maps.Geocoder();

    var markers = [

    <?php

    //orgnize fans by city
    $query = "SELECT city, state, COUNT(*) fans FROM users GROUP BY city ORDER BY fans DESC";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){

    //pulls the city, state code from the database and stores it as a string in $address
    $address = '"' . $row['city'] . ", " . $row['state'] . '"';


       //  "$row['city'].", ".$row['state']";


        geocoder.getLatLng($address, function(point) {
                           $latitude = point.y;
                           $longitude = point.x;
        });
        echo "{ lat: \"".$latitude.", lng: ".$longitude.", name: "'"'.$row['city'].", ".$row['state'].'"'"},\n ";
        echo "<br/>";
    }

    ?>

    ];

    //var locationNameArray = ['Portland','Seattle','Los Angeles','San Diego','Las Vegas'];

    // Create the markers ad infowindows.
    for (index in markers) addMarker(markers[index]);
    function addMarker(data) {
        // Create the marker
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.lng),
            map: map,
            title: data.name
        });

1 个解决方案

#1


1  

In your post you are mixing PHP with Javascript. You can't use a Javascript function geocoder.getLatLng inside a PHP tag.

在你的帖子中,你将PHP与Javascript混合。您不能在PHP标记内使用Javascript函数geocoder.getLatLng。

To fill the database with lat, lng information I suggest to use the geocoding with PHP. So you will need one table for cities.

要使用lat,lng信息填充数据库,我建议使用PHP进行地理编码。所以你需要一张桌子用于城市。

Table city:

id_city
name
state
lat
lng

And the user table have just a foreign key to id_city:

用户表只有id_city的外键:

Table users:

id_user
fi_city
...

And here is a quick and dirty example how to get the lat, lng information from Google geocoder with PHP:

这里有一个快速而肮脏的例子,如何使用PHP从Google地理编码器获取lat,lng信息:

$address = urlencode('New York');
$googleApi = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';

$json = file_get_contents(sprintf($googleApi, $address));

$resultObject = json_decode($json);

$location = $resultObject->results[0]->geometry->location;

$lat = $location->lat;
$lng = $location->lng;

echo 'Lat: '.$lat.', Lng: '.$lng;

There is no error checking but I hope you get the point. With this result you can update your database. I hope you know how to join multiple tables to get the result from users and city tables.

没有错误检查,但我希望你明白这一点。通过此结果,您可以更新数据库。我希望您知道如何连接多个表以从用户和城市表中获取结果。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/01/30/13d028b7f725dbdc9abbc171e332841d.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告