3050311118 发表于 2013-6-30 01:56:01

安卓可以根据IP地址算出经纬度,这是什么原理啊?(有证...

http://mobile.51cto.com/aprogram-395256.htm

Android由IP地址查询经纬度坐标实例
2013-05-23 14:43 佚名 jizhuomi 我要评论(1) 字号:T | T

本文简单粗暴,直接以代码的方式演示如何根据IP地址查询地理经纬度坐标位置。
AD:2013大数据全球技术峰会课程PPT下载
大家都知道,根据IP地址就可以知道它所在的具体位置,在Android中同样可以由IP地址得到它的位置,即具体的地理经纬度坐标。

本文就直接以代码的方式演示如何根据IP地址查询地理经纬度坐标位置,下面的例子中演示的就是由58.192.32.1这个IP地址查询到其所在的经纬度坐标为(118.777802,32.061699)。

package eoe.demo;   
import com.mapdigit.gis.DigitalMap;   
import com.mapdigit.gis.MapPoint;   
import com.mapdigit.gis.geometry.GeoLatLng;   
import com.mapdigit.gis.service.IIpAddressGeocodingListener;   
import com.mapdigit.gis.service.IpAddressLocation;   
import com.pstreets.gisengine.R;   
import com.pstreets.gisengine.SharedMapInstance;   
import android.app.Activity;   
import android.os.Bundle;   
import android.view.Menu;   
import android.view.MenuInflater;   
import android.view.MenuItem;   
public class MapIpSearch extends Activity implements   
IIpAddressGeocodingListener {   
@Override   
public void onCreate(Bundle savedInstanceState) {   
super.onCreate(savedInstanceState);   
setContentView(R.layout.main);   
}   
@Override   
public void onStart() {   
super.onStart();   
GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);   
SharedMapInstance.map.setCenter(center, 15,   
com.mapdigit.gis.raster.MapType.MICROSOFTCHINA);   
SharedMapInstance.map.setIpAddressGeocodingListener(this);   
}   
@Override   
public boolean onCreateOptionsMenu(Menu menu) {   
MenuInflater inflater = getMenuInflater();   
inflater.inflate(R.menu.mapgeocoding_menu, menu);   
return true;   
}   
@Override   
public boolean onOptionsItemSelected(MenuItem item) {   
switch (item.getItemId()) {   
case R.id.findaddress:   
SharedMapInstance.map.getIpLocations("58.192.32.1");   
return true;   
default:   
return super.onOptionsItemSelected(item);   
}   
}   
@Override   
public void done(String query, IpAddressLocation result) {   
if (result != null && result.error.length() == 0   
&& result.longitude.length() > 0   
&& result.longitude.length() > 0) {   
try {   
MapPoint mapPoint = new MapPoint();   
String latLng = "[" + result.longitude + "," + result.latitude+ ",0]";   
mapPoint.point = DigitalMap.fromStringToLatLng(latLng);   
mapPoint.setName(result.organization);   
mapPoint.setNote(result.city + " " + result.country);   
SharedMapInstance.map.panTo(mapPoint.point);   
} catch (Exception e) {   
result.error = "IP_NOT_FOUND";   
}   
}   
}   
@Override   
public void readProgress(int bytes, int total) {   
}   
}   

qwe2231695 发表于 2013-6-30 01:57:59

和qq地理位置一样原理吧

lhxr 发表于 2013-6-30 22:52:27

IP登记和数据库支持
Google具有先天优势

yyts 发表于 2013-6-30 23:08:27

要不要联网的,联网的话就简单了,百度都有提供这样的接口.IP定位,经纬度定位...

http://developer.baidu.com/map/webservice.htm

SNOOKER 发表于 2013-6-30 23:35:10

因为IP就是根据所在地区进行分配的,所以能通过IP确定你所在的地区

dongfo 发表于 2013-9-28 12:08:32

GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);   
为什么程序里直接给出了地址呢
页: [1]
查看完整版本: 安卓可以根据IP地址算出经纬度,这是什么原理啊?(有证...