使用Cordova / Angular JS从用户位置获取地址

时间:2015-12-19 17:06:36

标签: javascript angularjs cordova google-maps ionic-framework

使用cordova-plugin-geoloaction插件我正在检索用户在其移动设备上的位置。我的onSuccess()函数返回纬度和经度,现在我想在应用中显示相应的地址:

<script type="text/javascript" charset="utf-8">

  // Wait for device API libraries to load
  //
  document.addEventListener("deviceready", onDeviceReady, false);

  // device APIs are available
  function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
  }

  // onSuccess Geolocation
  function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: ' + position.coords.latitude              + '<br />' +
            'Longitude: '          + position.coords.longitude             + '<br />' +
            'Altitude: '           + position.coords.altitude              + '<br />' +
            'Accuracy: '           + position.coords.accuracy              + '<br />' +
            'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
            'Heading: '            + position.coords.heading               + '<br />' +
            'Speed: '              + position.coords.speed                 + '<br />' +
            'Timestamp: '          + position.timestamp                    + '<br />';
  }

  // onError Callback receives a PositionError object
  function onError(error) {
    alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
  }

</script>

我发现很多API(例如谷歌地图)提供纬度/经度转换的地址,但不是相反(纬度/经度来解决)。

我可以使用哪些好的网络服务吗?

1 个答案:

答案 0 :(得分:5)

您可以使用Google Maps Geocoding service按坐标解析地址(反向地理编码)

示例

function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 }
    });
    var geocoder = new google.maps.Geocoder;
    var infowindow = new google.maps.InfoWindow;

    document.getElementById('submit').addEventListener('click', function () {
        geocodeLatLng(geocoder, map, infowindow);
    });
}

function geocodeLatLng(geocoder, map, infowindow) {
    var input = document.getElementById('latlng').value;
    var latlngStr = input.split(',', 2);
    var latlng = { lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1]) };
    geocoder.geocode({ 'location': latlng }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                map.setZoom(11);
                var marker = new google.maps.Marker({
                    position: latlng,
                    map: map
                });
                infowindow.setContent(results[0].formatted_address);
                infowindow.open(map, marker);
            } else {
                window.alert('No results found');
            }
        } else {
            window.alert('Geocoder failed due to: ' + status);
        }
    });
}

google.maps.event.addDomListener(window, 'load', initMap);
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#map {
    height: 100%;
}

#floating-panel {
    position: absolute;
    top: 10px;
    left: 25%;
    z-index: 5;
    background-color: #fff;
    padding: 5px;
    border: 1px solid #999;
    text-align: center;
    font-family: 'Roboto','sans-serif';
    line-height: 30px;
    padding-left: 10px;
}
<script src="https://maps.google.com/maps/api/js"></script>
 <div id="floating-panel">
    <input id="latlng" type="text" value="40.714224,-73.961452">
    <input id="submit" type="button" value="Reverse Geocode">
</div>
<div id="map"></div>