問題描述
我有一個 Java 應用程序,我想找出訪問者的位置.API 會給我一個訪客的郵政編碼,然后基于該郵政編碼,我會找到一個查詢并在我的應用程序中獲取 25/50 英里半徑內的用戶.或者,訪問者可以輸入他們感興趣的郵政編碼,應用程序應該返回半徑為 x 英里的用戶.對于在系統中注冊的用戶,我們只有他們的郵政編碼.我查看了一些選項,但沒有找到確切的解決方案.我不想下載郵政編碼數據庫并維護它們.我不認為 Google API 支持這種東西.
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.
推薦答案
我找不到任何提供 API 的服務.經過大量研究,我發現您必須將郵政編碼及其緯度和經度位置下載到表格中.然后計算要搜索的半徑內的坐標.這是對我幫助很大的網站.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/
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/
如果你真的不想使用 php,這里是 java 中的計算
Here is the calculation in java if you really if you do not want to work with php
//這您將通過根據郵政編碼查詢數據庫來獲得雙緯度 = Double.parseDouble(zipCode.getLatitude());雙經度 = Double.parseDouble(zipCode.getLongitude());
//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));
這篇關于地理位置 API 和在半徑內查找用戶的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!