大家都知道,根據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) {
-
- }
-
- }