0

我正在尝试使用谷歌地图坐标在第三方网站的 XML 提要中使用,它应该创建地图本身。我使用 URL http://maps.google.com/maps/geo?q=hong%20kong&output=json&oe=utf8&sensor=false(单击此链接无效,但复制和粘贴有效)。这给了我点坐标 114.1094970 和 22.3964280。如果我将这些坐标直接放入谷歌地图,它们就不起作用,那我做错了什么?

我的函数本身的代码如下,但这似乎是谷歌地图的固有问题,而不是我的代码:

function getLatandLong($add)
{ 
$url = 'http://maps.google.com/maps/geo?q='.urlencode($add).'&output=json&oe=utf8&sensor=false';
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);


if(is_array($jsondata )&& $jsondata ['Status']['code'] == 200){
  $output['lat'] = $jsondata ['Placemark'][0]['Point']['coordinates'][0];
  $output['lng'] = $jsondata ['Placemark'][0]['Point']['coordinates'][1];
 }


    return $output;
}

任何帮助将非常感激!

4

2 回答 2

1

这些坐标是正确的,并且可以在 Google 地图中使用。

我猜这是因为您以错误的顺序传递它们:
您需要首先放置纬度坐标,因为
22.396428 114.109497
不是
114.109497 22.396428

实际上,在查看代码片段时,您会错误地解析它们:JSON 数据首先具有经度,其次具有纬度……因此,代码应为:

  $output['lat'] = $jsondata ['Placemark'][0]['Point']['coordinates'][1];
  $output['lng'] = $jsondata ['Placemark'][0]['Point']['coordinates'][0];
于 2012-10-25T03:48:55.990 回答
0

您可以使用谷歌地图地理坐标的 jquery 移动应用程序示例:


jsfiddle-google-maps-地理坐标

或在此处使用自定义图像:jsfiddle-google-map-with-images

头是:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<!--link rel="stylesheet" href="/Content/jquery-mobile-responsive.css" /-->
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true&language=en"></script>
<script>
    $(document).bind("mobileinit", function () {
        $.mobile.ajaxEnabled = false;
    });

    $(Start);

    function Start() {
        $("#google_map .ui-collapsible-heading").click(function () {
            var locations = [
              ['<br/>Main Office', 31.590496, 34.561981, 4],
              ['Sensor 16<br/>User Name: 16_DE-1R', 31.590595, 34.562980, 5],
              ['Sensor 17<br/>User Name: 17_TEN-2MP', 31.590694, 34.563979, 3],
              ['Sensor 18<br/>User Name: 18_TEN-2MP', 31.590793, 34.564978, 2],
            ];

            var map = new google.maps.Map(document.getElementById('googleMap'), {
                zoom: 17,
                center: new google.maps.LatLng(31.590892, 34.561977),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            var infowindow = new google.maps.InfoWindow();

            var marker, i;

            for (i = 0; i < locations.length; i++) {
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(locations[i][3], locations[i][2]),
                    map: map
                });

                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                    return function () {
                        infowindow.setContent(locations[i][0]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
            }
        });
    }
</script>

与身体:

<div data-role="page" class="type-interior">
<div data-role="header" data-theme="a">
    <a data-icon="back" href="#" rel="external">Back</a>
    <h1>Sensor</h1>
</div>
<div data-role="content">
    <div class="content-primary">
        <h2>Sensor: 16</h2>
            <div data-role="collapsible-set">
                <div id="google_map" data-role='collapsible' data-collapsed=true data-theme="b" data-content-theme="d">
                    <h1>Sensor Map</h1>
                    <div id="googleMap" style="width:100%; height:300px;"></div>
                </div>
            </div>    
        <ul data-role="listview" data-inset="true" data-theme="b">
            <li data-role="list-divider"></li>                
            <li><a href="#" rel="external">Configure Comm</a></li>
            <li><a href="#" rel="external">Measurements</a></li>
            <li><a href="#" rel="external">Users</a></li>
        </ul>
    </div>
    <div class="content-secondary">
        <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
                <h3>More Options</h3>
                <ul data-role="listview" data-theme="c" data-dividertheme="d">
                    <li data-role="list-divider">Actions</li>
                    <li><a href="/Home/UserList?back=Index" rel="external">Add User</a></li>
                    <li><a href="#" rel="external">Edit Sensor</a></li>
                    <li><a href="#" rel="external">Delete Sensor</a></li>
                </ul>
        </div>
    </div>
</div>
<div data-role="footer" data-theme="a">
    <h4>Mobile</h4>
</div>

于 2012-12-27T16:02:28.483 回答