大家都知道,根据IP地址就可以知道它所在的具体位置,在Android中同样可以由IP地址得到它的位置,即具体的地理经纬度坐标。
本文就直接以代码的方式演示如何根据IP地址查询地理经纬度坐标位置,下面的例子中演示的就是由58.192.32.1这个IP地址查询到其所在的经纬度坐标为(118.777802,32.061699)。
Java代码
- 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) {
-
- }
-
- }
Android中由IP地址查询经纬度坐标的实例,布布扣,bubuko.com
Android中由IP地址查询经纬度坐标的实例
原文:http://www.cnblogs.com/xiaochao1234/p/3829496.html