Blackberry引路蜂地图开发示例:IP地址查询

IP地址查询,可以根据IP地址查询到该IP所在的地理经纬度坐标,比如下述例子查询IP 地址58.192.32.1,所在经纬度为118.777802,32.061699,为南京大学所在地。

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo.rim;
//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.DigitalMap;
import com.mapdigit.gis.MapPoint;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.mapdigit.gis.service.IIpAddressGeocodingListener;
import com.mapdigit.gis.service.IpAddressLocation;
import com.pstreets.gisengine.demo.MapDemoRIM;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem;
//[------------------------------ MAIN CLASS ----------------------------------]
public class MapIpSearchRIM extends MapDemoRIM implements
  IIpAddressGeocodingListener   {
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MapIpSearchRIM theApp = new MapIpSearchRIM();
        theApp.enterEventDispatcher();
    }
    private MenuItem mapFindAddressMenuItem = new MenuItem("Find Address", 0, 0){
        public void run(){
           map.getIpLocations("58.192.32.1");
        }
    };
    public MapIpSearchRIM() {
        init();
        pushScreen(canvas);
        map.setIpAddressGeocodingListener(this);
       GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
        map.setCenter(center, 13, MapType.MICROSOFTCHINA); 
    }
   public void done(String query, IpAddressLocation result) {
        if (result != null && result.error.length() == 0
                && result.longitude.length() > 0
                && result.latitude.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);
                map.panTo(mapPoint.point);
            } catch (Exception e) {
                result.error = "IP_NOT_FOUND";
            }
        }
    }
    protected void createMenu(Menu menu, int instance){
         menu.add(mapFindAddressMenuItem);
   }
}

注:目前IP查询结果总是以英文返回,如上述结果详细内容。
ISP:”China Education and Research Network”
Organization: “Nan Jing University”
Country: “CN”
City: “Nanjing”
若想知道该经纬度对应的地名,可以使用地址反编码服务。
你可以输入 127.0.0.1 查询本机地址。

Blackberry引路蜂地图开发示例:本地查询

本地查询可以查询指定区域内诸如宾馆,邮局等用户感兴趣的地方。其使用方法和地址查询非常类似。
下面示例查询32.0616667, 118.7777778为中心点一定区域内宾馆的信息。

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo.rim;

//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.MapPoint;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.mapdigit.gis.service.IGeocodingListener;
import com.pstreets.gisengine.demo.MapDemoRIM;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem;


//[------------------------------ MAIN CLASS ----------------------------------]
/**
 *  map pan demo for Guidebee Map API on MIDP platform.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 09/02/11
 * @author      Guidebee Pty Ltd.
 */
public class MapLocalSearchRIM extends MapDemoRIM implements  IGeocodingListener {

    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MapLocalSearchRIM theApp = new MapLocalSearchRIM();       
        theApp.enterEventDispatcher();
    }
    
    private MenuItem mapFindAddressMenuItem = new MenuItem("Find Address", 0, 0){
        public void run(){
            String name = "宾馆";                 
            GeoLatLng screenCenter = map.getScreenCenter();                
            map.getLocations(name, 0, screenCenter,                        
            map.getScreenBounds(screenCenter)); 
        }
    };

    public MapLocalSearchRIM() {

        init();
        pushScreen(canvas);
        map.setGeocodingListener(this);
       GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);        
        map.setCenter(center, 13, MapType.MICROSOFTCHINA); 
        
    }

   
    public void done(String arg0, MapPoint[] result) {
        if (result != null) {            
            map.panTo(result[0].getPoint());            
            for (int i = 0; i < result.length; i++) {                
                System.out.println(result[i].objectNote);            
             }        
         } 
    }
    
    protected void createMenu(Menu menu, int instance){
         menu.add(mapFindAddressMenuItem);
   }
}

本地查询方法
public void getLocations(String address,int start,GeoLatLng center,GeoBounds bound, IGeocodingListener listener);
指定中心点和查询区域。本地查询可以多次返回结果,start为查询结果起始顺序,每次返回结果在SearchOptions 中定义,缺省每次返回4个。
上述结果返回内容为,具体结果可能不同。
“晶丽酒店025-83310818 025-83310818212223 025-86636636 ”
“城市名人酒店025-83123333 025-83123888 ”
“南京玄武饭店 025-83358888 ”
“Sofitel Galaxy Nanjing – 南京索菲特银河大酒店025-83718888 “

Blackberry引路蜂地图开发示例:路径查询

可以通过RasterMap的getDirection()方法来查询路径,和查询地址类似,路径查询的结果也是通过回调函数的方式来通知应用程序的,下面的例子返回南京到北京的路径。返回结果存放在MapDirection中,MapDirection包含了路径的详细信息,包括路径的每个步骤,长度,时间,方向等。 

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo.rim; 

//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.MapDirection;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.mapdigit.gis.service.IRoutingListener;
import com.pstreets.gisengine.demo.MapDemoRIM;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem; 

//[------------------------------ MAIN CLASS ----------------------------------]
/**
 *  Map routing demo for Guidebee Map API on RIM platform.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 10/02/11
 * @author      Guidebee Pty Ltd.
 */
public class MapRoutingRIM extends MapDemoRIM implements    IRoutingListener { 

    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MapRoutingRIM theApp = new MapRoutingRIM();      
        theApp.enterEventDispatcher();
    }
   
    private MenuItem mapGetDirectionMenuItem = new MenuItem("Get Direction", 0, 0){
        public void run(){
            String name1 = "南京";           
            String name2 = "北京";           
            map.getDirections("from: " + name1 + " to: " + name2);
        }
    }; 

    public MapRoutingRIM() { 

        init();
        pushScreen(canvas);
        map.setRoutingListener(this);
        GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);       
        map.setCenter(center, 13, MapType.MICROSOFTCHINA);
       
    } 

    public void done(String arg0, MapDirection result) {
        if (result != null) {
            map.setMapDirection(result);
            map.resize(result.getBound());
            map.setZoom(5);
        }
    }
   
    protected void createMenu(Menu menu, int instance){
         menu.add(mapGetDirectionMenuItem); 

   }
}

地图服务可以选择使用Google 地图服务,CloudMade地图服务,在中国还可能选择MapAbc地图服务,缺省使用Google 地图服务。
getDirections()具有三个重载函数,例子中是采用的文字描述方式。上述示例采用了from: address1 to: address2 的格式, CloudMade地图服务和MapAbc地图服务则必需采用 经度1,纬度1,经度2,纬度2和格式。
为避免混淆,可以使用下述格式。
public void getDirection(GeoLatLng[] waypoints, IRoutingListener listener);
其中 waypoints 为途径点坐标数组经纬值,可以支持多点路径查询。
此外对于MapAbc 地图服务,还可以指定城市编码,如南京编码为25。
public void getDirection(int citycode,String query, IRoutingListener listener);

Blackberry引路蜂地图开发示例:地址查询

引路蜂地图API中同样提供了地址查询,路径查询,本地搜索,IP地址查询,地址反编码(通过经纬度查地名)等。 地址查询(或称为地址编码)是将输入的地名(如南京林业大学)转换成对应的经纬度坐标然后将其显示在地图上。 

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo.rim;

//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.MapPoint;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.mapdigit.gis.service.IGeocodingListener;
import com.pstreets.gisengine.demo.MapDemoRIM;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem;
//[------------------------------ MAIN CLASS ----------------------------------]
/**
 *  map pan demo for Guidebee Map API on MIDP platform.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 09/02/11
 * @author      Guidebee Pty Ltd.
 */
public class MapGeocodingRIM extends MapDemoRIM implements  IGeocodingListener {

    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MapGeocodingRIM theApp = new MapGeocodingRIM();      
        theApp.enterEventDispatcher();
    }
   
    private MenuItem mapFindAddressMenuItem = new MenuItem("Find Address", 0, 0){
        public void run(){
            map.getLocations("南京林业大学");
        }
    };

    public MapGeocodingRIM() {

        init();
        pushScreen(canvas);
        map.setGeocodingListener(this);
       GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);       
        map.setCenter(center, 13, MapType.MICROSOFTCHINA);
       
    }

  
    public void done(String arg0, MapPoint[] result) {
        if (result != null) {
            map.panTo(result[0].getPoint());
        }
    }
   
    protected void createMenu(Menu menu, int instance){
         menu.add(mapFindAddressMenuItem);

                        }
}

所有的地图服务都是采用异步方式调用,在调用RasterMap.getLocation(address)前,需要设置好返回结果时的回调函数RasterMap.setGeocodingListener,回调函数接口定义为IGeocodingListener。 回调方法为public void done(String query,MapPoint[] result) ,如果查询结果不为空,则reusult 为查询结果的数组。示例中将地图转到第一个查询结果。

对于MapAbc 地图服务,还可以指定城市编码,如南京编码为25。
public void getLocation(int citycode,String query, IGeocodingListener listener);

Blackberry引路蜂地图开发示例:放大、缩小

RasterMap的 zoomIn,zoomOut 用来放大缩小地图。 


package com.pstreets.gisengine.demo.rim;

//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.pstreets.gisengine.demo.MapDemoRIM;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem;

//[------------------------------ MAIN CLASS ----------------------------------]
/**
 *  map zoom demo for Guidebee Map API on MIDP platform.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 09/02/11
 * @author      Guidebee Pty Ltd.
 */
public class MapZoomRIM extends MapDemoRIM {

    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MapZoomRIM theApp = new MapZoomRIM();      
        theApp.enterEventDispatcher();
    }
   
    private MenuItem mapZoomInMenuItem=new MenuItem("Zoom In",0,0){
          public void run(){
              map.zoomIn();
            }
        };
    private MenuItem mapZoomOutMenuItem=new MenuItem("Zoom Out",1,0){
          public void run(){
              map.zoomOut();
            }
        };

    public MapZoomRIM() {
        init();
        pushScreen(canvas);
        GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
        map.setCenter(center, 13, MapType.MICROSOFTCHINA);

    }

     protected void createMenu(Menu menu, int instance){
         menu.add(mapZoomInMenuItem);
         menu.add(mapZoomOutMenuItem);
                        }
}

Blackberry引路蜂地图开发示例:地图平移

RasterMap 有两个方法可以用于平移地图,panTo 将地图移动到指定经纬度坐标,panDirection(dx,dy) 将地图从当前位置平移dx,dy 个象素。
下列示例可以上,下,左,右平移地图。

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo.rim;

//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.pstreets.gisengine.demo.MapDemoRIM;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem;

//[------------------------------ MAIN CLASS ----------------------------------]
/**
 *  map pan demo for Guidebee Map API on MIDP platform.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 09/02/11
 * @author      Guidebee Pty Ltd.
 */
public class MapPanRIM extends MapDemoRIM  {

   /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MapPanRIM theApp = new MapPanRIM();      
        theApp.enterEventDispatcher();
    }
   
    private MenuItem mapUpMenuItem = new MenuItem("Up", 0, 0){
        public void run(){
            map.panDirection(0, -32);

            }};
    private MenuItem mapDownMenuItem = new MenuItem("Down",1, 0){
        public void run(){
            map.panDirection(0, 32);
            }};
    private MenuItem mapLeftMenuItem = new MenuItem("Left",2, 0){
        public void run(){
            map.panDirection(-32, 0);
            }};
    private MenuItem mapRightMenuItem = new MenuItem("Right", 3, 0){
        public void run(){
            map.panDirection(32, 0);
            }};

    public MapPanRIM() {

        init();
        pushScreen(canvas);
        GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
        map.setCenter(center, 13, MapType.MICROSOFTCHINA);

    }

    protected void createMenu(Menu menu, int instance){
         menu.add(mapUpMenuItem);
         menu.add(mapDownMenuItem);
         menu.add(mapLeftMenuItem);
         menu.add(mapRightMenuItem);

    }
}