Geo Location API and finding user within a radius

半世苍凉 提交于 2019-12-01 08:14:43

问题


I have a Java application and I would like to find out the location of a visitor. The API would give me a the zip code of visitor and then based on that zip code, I will find fire a query and get users in my application within 25/50 mile radius. Alternatively visitor can type in the zip code of their interest and application should return users withing x mile of radius. For the users who are registered in the system, we have only their zip code. I have looked around some options but not found the exact solution. I do not want to download the database of zip code and maintain them. I do not think Google API support this kind of stuff.


回答1:


I don't know how accurate it is, but have you looked at IPinfoDB? They'll return XML and JSON results but you'll need to register for an API key. It doesn't provide anything for a radius though, but for nearby zipcodes, you can use something like this: http://www.geonames.org/export/web-services.html#findNearbyPostalCodes




回答2:


I could not find any service which offers API . After much research i found that you will have to download zip codes along with their latitude and longitude positions into a table. Then calculate co ordinates within the radius you want to search. This is the website which helped me a lot. http://www.dougv.com/2009/03/27/getting-all-zip-codes-in-a-given-radius-from-a-known-point-zip-code-via-php-and-mysql/

Here is the calculation in java if you really if you do not want to work with php

//this you will get by querying the database against the zip code Double latitude = Double.parseDouble(zipCode.getLatitude()); Double longitude = Double.parseDouble(zipCode.getLongitude());

    Double latN =Math.asin(
            Math.sin(Math.toRadians(latitude)) * Math.cos(distance/radius) + 
            Math.cos(Math.toRadians(latitude)) * Math.sin(distance/radius) * Math.cos(Math.toRadians(0)));


    Double latS =Math.asin(
            Math.sin(Math.toRadians(latitude)) * Math.cos(distance/radius) + 
            Math.cos(Math.toRadians(latitude)) * Math.sin(distance/radius) * Math.cos(Math.toRadians(180)));


    Double longE = Math.toRadians(longitude) + 
                    Math.atan2(
                            Math.sin(Math.toRadians(90)) * Math.sin(distance/radius)* Math.cos(Math.toRadians(latitude)) 
            , Math.cos(Math.toRadians(distance/radius)) - Math.sin(Math.toRadians(latitude))* Math.sin(Math.toRadians(latN)) );

    Double longW = Math.toRadians(longitude) + 
    Math.atan2(
                    Math.sin(Math.toRadians(270)) * Math.sin(distance/radius)* Math.cos(Math.toRadians(latitude)) 
                , Math.cos(Math.toRadians(distance/radius)) - Math.sin(Math.toRadians(latitude))* Math.sin(Math.toRadians(latN)) );

    System.out.println("Latutude N "+Math.toDegrees(latN) +" Latitide S "+Math.toDegrees(latS) +">>> Longitude E "+Math.toDegrees(longE) +" Longitude W "+Math.toDegrees(longW));


来源:https://stackoverflow.com/questions/6977322/geo-location-api-and-finding-user-within-a-radius

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!