地图和GPS会是比较常用的一个功能了,网上能找到相当多的示例,同时几个地图引擎的公司也有开放他们的地图SDK,可以加以利用。这次是重点研究了基于Rexsee实现地图路线控制这个功能,分享出来。同时把Rexsee写的GPS功能源码也全部贴出来,有点多。看不明白的自己去Rexsee的社区:

Rexsee在扩展中直接使用了高德的SDK,应该是合作关系吧。通过高德地图对象内的函数,可以很轻松的制作出类似谷歌地图这样的导航软件。在地图插件打开后,当数据没有在插件框体内加载完毕前关闭插件或按下回退键,极易造成程序崩溃。所以再打开插件的时候最好设置为不可回退取消。

Rexsee因为是基于Web的开发,所以可以将程序做成全机型自动适配,那么在调整窗口的位置的时候就需要在位置上编写一定的逻辑。当然,屏幕像素获取方式不同,值也不同。。并且,在平板上,有的没有通知栏,或者通知栏在下面,有的需要减去通知栏的高度,有的则不需要。这都是需要去判断的。

当然在测试的时候可以先不管这些,随便设置一个就好。
rexseeMapAbc.start('window-cancelable:false;window-moveable:false;border-width:10px;border-(2*rexseeScreen.getScreenDensityScale()))+';window-modeless:true;window-dim-amount:0;');

基本操作中还包含了中心点设置,定位,路况。路况目前打开后无法消除。。。不知道为什么。

高德地图中的数据操作,导航操作,兴趣点操作返回值都是已数组的形式(前提是EVAL过)出现的,也就是所在导航时有多条。由于导航需要提供2地的经纬度,所以请大家配合数据转换中的地名获得目的地信息的函数方法使用,当然最好是限制经纬度范围,否则必须要拼写详细,如XX省XX市XX地名。请求出来的则是数跳线路,请根据需要取其中的1条或生成选择菜单。

function getRoute()
{
rexseeMapAbc.hideRoute();
var places=eval('('+prompt('prompt','title=导航输入框;message=请输入起始地和目的地:;options=起始地:|目的地:;defaultValue=北京市北京西站|北京市北京南站;inputType=text|text;')+')');
var startPlace=places[0];
var endPlace=places[1];
var tempsp=eval('('+rexseeMapAbc.getAddressFromLocationName(startPlace,1)+')');
rexseeMapAbc.requestRoute('routeRequest01',splong,splat,eplong,eplat,role);
}
  
function onRouteReady(id){
// alert(rexseeMapAbc.getRequesedRoute(id));
var temps=rexseeMapAbc.getRequestedRoute(id);
//temps = temps.replace(/\r\n","");

// alert(temps);
rexseeMapAbc.showRoute(id,0)
}


兴趣点的控制和路线控制属于同一个,如果关闭兴趣点,那么地图上的导航线也会一起消失。高德地图如果配合GPS使用的话,需要按照国家标准偏移,请偏移后(对象有提供相应函数)再进行定位,否则会有偏差。

还要注意:提供经纬度作为参数时,请先转换为字符串,不然偏差很大。

最后是Rexsee写的原生GPS源码,搞原生的可以直接看这个




/* 
    * Copyright (C) 2011 The Rexsee Open Source Project * * You may obtain a copy of the License at * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * limitations under the License.  
   */ 
     
   package 
    rexsee.location;  
   import 
    java.util.List;  
   import 
    rexsee.core.browser.JavascriptInterface;  
   import 
    rexsee.core.browser.RexseeBrowser;  
   import 
    rexsee.core.browser.ActivityResult.ActivityResultListener; mport android.os.Bundle;  
   import 
    android.provider.Settings;  
   public 
     
   class 
    RexseeGps  
   implements 
    JavascriptInterface {  
   private 
     
   static 
     
   final 
    String INTERFACE_NAME  
   = 
     
   " 
   Gps 
   " 
   ; @Override  
   public 
    String getInterfaceName() {  
   return 
    mBrowser.application.resources.prefix  
   + 
    INTERFACE_NAME; } @Override  
   public 
     
   static 
     
   final 
    String EVENT_ONGPSLOCATIONCHANGED  
   = 
     
   " 
   onGpsLocationChanged 
   " 
   ;  
   public 
     
   static 
    String address2Json(Address address) {  
   if 
    (address  
   == 
     
   null 
   )  
   return 
     
   " 
   {} 
   " 
   ; String rtn  
   = 
     
   "" 
   ; rtn  
   += 
     
   " 
   { 
   " 
   ; rtn  
   += 
     
   " 
   \"Premises\":\" 
   " 
     
   + 
    address.getPremises()  
   + 
     
   " 
   \" 
   " 
   ;  
   // 
   Type of the location. Cross, road or POI.  
   // 
   rtn += ",\"SubLocality\":\"" + address.getSubLocality() + "\"";Not support SDK3  
     
    rtn  
   += 
     
   " 
   ,\"Thoroughfare\":\" 
   " 
     
   + 
    address.getThoroughfare()  
   + 
     
   " 
   \" 
   " 
   ; rtn  
   += 
     
   " 
   } 
   " 
   ;  
   return 
    rtn; }  
   public 
     
   static 
    String addresses2Json(List 
   < 
   Address 
   > 
    addresses) { String rtn  
   = 
     
   " 
   [ 
   " 
   ;  
   for 
    ( 
   int 
    i  
   = 
     
   0 
   ; i  
   < 
    addresses.size(); i 
   ++ 
   ) {  
   if 
    (i  
   != 
     
   0 
   ) rtn  
   += 
     
   " 
   , 
   " 
   ; }  
   private 
     
   int 
    minUpdateDuration  
   = 
     
   60 
   ;  
   public 
    RexseeGps( 
   final 
    RexseeBrowser browser) { mContext  
   = 
    browser.getContext(); mBrowser  
   = 
    browser; browser.eventList.add(EVENT_ONGPSSETTINGSUCCESSED);  
   public 
     
   void 
    run() { stop(); } }); mListener  
   = 
     
   new 
    LocationListener() { @Override  
   public 
     
   void 
    onLocationChanged(Location location) {  
   if 
    (browser  
   != 
     
   null 
   ) { browser.eventList.run(EVENT_ONGPSLOCATIONCHANGED,  
   new 
    String[]{ } @Override  
   public 
     
   void 
    onProviderDisabled(String provider) { } @Override  
   public 
     
   void 
    onStatusChanged(String provider,  
   int 
    status, Bundle extras) { } } }; }  
   // 
   JavaScript Interface  
     
     
   public 
     
   boolean 
    isReady() {  
   return 
    (((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).isProviderEnabled(LocationManager.GPS_PROVIDER))  
   ? 
     
   true 
    :  
   false 
   ; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minUpdateDuration  
   * 
     
   1000 
   , minUpdateDistance, mListener); }  
   else 
    { mBrowser.exception(getInterfaceName(),  
   " 
   GPS is not available. 
   " 
   ); } }  
   public 
     
   void 
    stop() { }  
   public 
     
   void 
    setMinimumUpdateDuration( 
   int 
    seconds) { minUpdateDuration  
   = 
    seconds; }  
   public 
     
   void 
    setMinimumUpdateDistance( 
   int 
    meter) { minUpdateDistance  
   = 
    meter; } }  
   public 
    String getLastKnownLocation() {  
   if 
    ( 
   ! 
   isReady())  
   return 
     
   " 
   {} 
   " 
   ;  
   try 
    { Location location  
   = 
    ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getLastKnownLocation(LocationManager.GPS_PROVIDER);  
   if 
    (location  
   == 
     
   null 
   )  
   return 
     
   "" 
   ;  
   return 
    rtn; }  
   catch 
    (Exception e) { mBrowser.exception(getInterfaceName(), e);  
   return 
     
   " 
   {} 
   " 
   ; } }  
   public 
    String getLastKnownGeo( 
   int 
    maxNumber) {  
   if 
    ( 
   ! 
   isReady())  
   return 
     
   " 
   [] 
   " 
   ;  
   try 
    { Location location  
   = 
    ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getLastKnownLocation(LocationManager.GPS_PROVIDER); (), location.getLongitude(), maxNumber);  
   return 
    addresses2Json(addresses); }  
   catch 
    (IOException e) { mBrowser.exception(getInterfaceName(), e);  
   return 
     
   " 
   [] 
   " 
   ; } }  
   public 
     
   float 
    getDistanceBetween(String startLongitude, String startLatitude, String endLongitude, String endLatitude) { Location.distanceBetween(sLatitude, sLongitude, eLatitude, eLongitude, results);  
   return 
    results[ 
   0 
   ]; } }