how to find lat ,long of a given area using pin co

2019-08-03 01:58发布

问题:

hi i am using Bing map in my website and want lat long to pinpoint that location on Bing map. my question is in my website how can i get lat long of an area using pincode. Is there any api that i can use to query lat long with by giving area pin code. i want to do it in backend. using some ajax call to that particular web api and get back lat lang and saved it into database so that i can use that lat long to plot location on my bing map.

i am using bing map 7 where i need to put lat long value into a json object and pass it into Bing map.

function GetMap()
{
var mapOptions = {
credentials: "Your Bing Maps Key",
center: new Microsoft.Maps.Location(47.592, -122.332), // i want these value in my         database 
mapTypeId: Microsoft.Maps.MapTypeId.birdseye,
zoom: 17,
showScalebar: false
}
var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), mapOptions);
}  

i can hard code it but in my application i have given client an option which is use to add new location so in back end i want it like when client add any new location it automatically save its lat long also.

thanks

回答1:

Using pincodes for obtaining the lat, long values may not be the best solution, since pin codes though popular for more than a century are still not a standard round the world. for example in US pincodes (zip codes) are normally have 5 digits (i.e. 06160) and in my part of the world their are normally 6 digits or more..

Though you can use combination of street address, state, country and pin code to find out the nearly correct geo coordinates for almost every part of the world. See following script which calls google api for finding the Lat, Long value... Here by I donate this code to the community under GPL:-

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> </script>

<script src="http://maps.google.com/maps?file=api&v=3&key=ABQIAAAA7j_Q-rshuWkc8HyFI4V2HxQYPm-xtd00hTQOC0OXpAMO40FHAxT29dNBGfxqMPq5zwdeiDSHEPL89A" type="text/javascript"> </script>
 <script type="text/javascript">

        var geocoder, location1;

        function initialize() {
            geocoder = new GClientGeocoder();
        }
        function prepareQuery(){
            var query='';
            var a1 = document.getElementById('address1').value;
            var a2 = document.getElementById('address2').value;
            var a3 = document.getElementById('address3').value;
            var cty = document.getElementById('city').value;
            var stat = document.getElementById('state').value;
            var cntry =document.getElementById('country').value;
            var pin = document.getElementById('pincode').value;

            if(a1 != null && a1!=''){
                query = query + a1 + ",";                    
            }
            if(a2 != null && a2!=''){
                query =query + a2 + ",";                    
            }
            if(a3 != null && a3!=''){
                query = query + a3 + ",";                    
            }
            if(cty != null && cty!=''){
                query = query + cty + ",";                    
            }
            if(stat != null && stat!=''){
                query = query + stat + ",";                    
            }
            if(cntry != null && cntry!=''){
                query = query + cntry + ",";                    
            }
            if(pin != null && pin!=''){
                query = query + pin + ",";                    
            }
            //                alert("Prepare Query Returns " +query);
            return query;
        }
        function submitFunc(){
            var location = prepareQuery();               
            geocoder.getLocations(location, function (response) {
                if (!response || response.Status.code != 200)
                {
                    alert("Sorry, we were unable to geocode the address");
                }
                else
                {                     
                    location1 = {latitude: response.Placemark[0].Point.coordinates[1], longitude: response.Placemark[0].Point.coordinates[0], Address : response.Placemark[0].address};

                    var items = [];
                    $.each(location1, function(key, value){                          
                        items.push('<li id="' + key + '">' + key +" : "+ value + '</li>');
                    });
                    //                        alert(items)
                    $("body").append("Results is :-");
                    $('<ul/>', {
                        'class': 'my-new-list',
                        html: items.join('')
                    }).appendTo('body');
                }
            });
        }

    </script>

and the html looks like....

        Line 1   : <input type="text" id="address1" value="" placeholder="Enter first line of address."/> <br></br>               
        Line 2   :<input type="text" id="address2" value="" placeholder="Enter second line of address."/> <br></br>               
        Line 3   :<input type="text" id="address3" value="" placeholder="Enter third line of address."/> <br></br>               
        City     : <input type="text" id="city" value="" placeholder="Enter city name."/> <br></br>               
        State    : <input type="text" id="state" value="" placeholder="Enter state name."/> <br></br>               
        Country  : <input type="text" id="country" value="" placeholder="Enter country name."/> <br></br>               
        Pin Code : <input type="text" id="pincode" value="" placeholder="Enter pincode value."/> <br></br>               
        <input type="button" name="submitBtn" value="submit" onclick='submitFunc();'/> <br></br>

        <h3> <strong> Your Results Will be displayed Here . . . .</strong> </h3>


回答2:

Geonames is a good service for all kind of location stuff, take a look at the list of the services they provide:

http://www.geonames.org/export/ws-overview.html

Could you use the one to look up postal codes? api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=demo