Question

I'm trying to use google maps coordinates for use in an XML feed for a third party site, which should create the map itself. Im using the URL http://maps.google.com/maps/geo?q=hong%20kong&output=json&oe=utf8&sensor=false (clicking this link won't work, but copying and pasting does). This gives me the point coordinates 114.1094970 and 22.3964280. If I put these coordinates straight into google maps they don't work, so what am I doing wrong?

My code for the function itself is as follows, but it seems to be an inherent problem with google maps rather than my code:

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;
}

Any help would be much appreciated!

Was it helpful?

Solution

These coordinates are correct and work in Google Maps.

I'm guessing this is because you pass them in the wrong order:
you need to put the latitude coordinate first, as in
22.396428 114.109497
not
114.109497 22.396428

Indeed in looking at the code snippet, your parse them incorrectly: the JSON data has the longitude first and the latitude second... Code should therefore read:

  $output['lat'] = $jsondata ['Placemark'][0]['Point']['coordinates'][1];
  $output['lng'] = $jsondata ['Placemark'][0]['Point']['coordinates'][0];

OTHER TIPS

you can use this example of jquery mobile app using google maps geo coordinates:


jsfiddle-google-maps-geo-coordinates

or with custom images here: jsfiddle-google-map-with-images

the head is:

<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>

and body:

<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>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top