Get LatLng from Zip Code - Google Maps API

前端 未结 6 715
我在风中等你
我在风中等你 2020-12-04 09:09

All I want is some simple example code that shows me how to obtain a latlng element from an inputted zip code OR a city/state.

6条回答
  •  不思量自难忘°
    2020-12-04 09:45

    Here is the function I am using for my work

    function getLatLngByZipcode(zipcode) 
    {
        var geocoder = new google.maps.Geocoder();
        var address = zipcode;
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                alert("Latitude: " + latitude + "\nLongitude: " + longitude);
            } else {
                alert("Request failed.")
            }
        });
        return [latitude, longitude];
    }
    

提交回复
热议问题