问题描述
为什么会提示有未结束的字符串常量和GRulerControl未定义呢<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:v="urn:schemas-microsoft-com:vml"><head><metahttp-equiv="content-type"content="text/html;charset=utf-8"/><title>123</title><scriptsrc="http://ditu.google.com/maps?file=api&v=2&key=ABQIAAAAUbR6g0RGGvD3wc8nEOCNZhQ1-r6OYR-mHk-jCzYLxV7O5V1vkBR9GY27V0NNKkh2ASTbqJMDYJeu2A"type="text/javascript"></script><!--包含自定义控件的源文件。引入GRulerControl控件。--><scriptsrc="ruler.js"type="text/javascript"></script><scripttype="text/javascript">//<![CDATA[//Copyright2007GoogleInc.//AllRightsReserved.functionload(){if(GBrowserIsCompatible()){varmap=newGMap2(document.getElementById('map'));//给地图添加内置的控件,分别为://平移及缩放控件(左上角)、比例尺控件(左下角)、缩略图控件(右下角)map.addControl(newGLargeMapControl());map.addControl(newGScaleControl());map.addControl(newGOverviewMapControl());//添加自定义的控件map.addControl(newGRulerControl());//将视图移到自己定义的位置map.setCenter(newGLatLng(43.832,125.322),14);}}//]]></script></head><bodyonload="load()"onunload="GUnload()"><divid="intro"style="width:700px;"><p><b>提示:</b>单击地图添加多个标记,设定您的路径。您还可以随意地修改现有路径。试试看在已有的标记上单击,或者拖拽它们!</p></div><divid="map"style="width:700px;height:500px;"></div></body></html>
functionGRulerControl(){varme=this;//可国际化的字符串me.RESET_BUTTON_TITLE_='清除所有测距标记';me.ENABLE_BUTTON_TITLE_='添加测距标记已启用,单击这里禁用';me.DISABLE_BUTTON_TITLE_='添加测距标记已禁用,单击这里启用';me.DELETE_BUTTON_TITLE_='删除';me.RESET_BUTTON_IMAGE_='images/ruler_clear.png';me.ENABLE_BUTTON_IMAGE_='images/ruler_enabled.png';me.DISABLE_BUTTON_IMAGE_='images/ruler_disabled.png';me.BACKGROUND_IMAGE_='images/ruler_background.png'me.KILOMETER_='公里';me.METER_='米';}GRulerControl.prototype=newGControl();/***初始化标尺控件*/GRulerControl.prototype.initialize=function(map){varme=this;varcontainer=document.createElement('div');me.setButtonStyle_(container);//“启用/禁用”按钮varbtnEnable=document.createElement('img');btnEnable.width=btnEnable.height=19;GEvent.addDomListener(btnEnable,'click',function(){me.setEnabled(!me.isEnabled());});container.appendChild(btnEnable);//“重置”按钮varbtnReset=document.createElement('img');btnReset.width=btnReset.height=19;btnReset.src=me.RESET_BUTTON_IMAGE_;btnReset.title=me.RESET_BUTTON_TITLE_;GEvent.addDomListener(btnReset,'click',function(){me.reset();});container.appendChild(btnReset);//距离标签vartxtInfo=document.createElement('div');txtInfo.style.font='smallArial';txtInfo.style.fontWeight='bold';txtInfo.style.fontSize='9pt';txtInfo.style.width='82px';container.appendChild(txtInfo);//初始化内部变量map.rulerControl_=me;me.map_=map;me.head_=newObject();me.tail_=newObject();me.head_.next_=me.tail_;me.tail_.prev_=me.head_;me.btnEnable_=btnEnable;me.btnReset_=btnReset;me.txtInfo_=txtInfo;me.setEnabled(true);map.getContainer().appendChild(container);returncontainer;}/***设置控件的格式*/GRulerControl.prototype.setButtonStyle_=function(button){button.style.backgroundImage='url('+this.BACKGROUND_IMAGE_+')';button.style.font='smallArial';button.style.border='1pxsolid#888888';button.style.padding='4px';button.style.textAlign='right';button.style.cursor='pointer';}GRulerControl.prototype.formatDistance_=function(len){varme=this;len=Math.round(len);if(len<=1000){returnlen+''+me.METER_;}elseif(len<=1000000){returnlen/1000+''+me.KILOMETER_;}returnMath.round(len/1000)+''+me.KILOMETER_;}GRulerControl.prototype.formatDegree_=function(value){value=Math.abs(value);varv1=Math.floor(value);varv2=Math.floor((value-v1)*60);varv3=Math.round((value-v1)*3600%60);returnv1+'°'+v2+'''+v3+'"';}GRulerControl.prototype.formatLatLng_=function(pt){varme=this;varlatName,lngName;varlat=pt.lat();varlng=pt.lng();latName=lat>=0?'北纬':'南纬';lngName=lng>=0?'东经':'西经';returnlngName+me.formatDegree_(lng)+','+latName+me.formatDegree_(lat);}GRulerControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_TOP_RIGHT,newGSize(8,8));}/***返回控件是否已启用*/GRulerControl.prototype.isEnabled=function(){returnthis.enabled_;}GRulerControl.prototype.setEnabled=function(value){varme=this;if(value==me.enabled_)return;me.enabled_=value;if(me.enabled_){me.mapClickHandle_=GEvent.addListener(me.map_,'click',me.onMapClick_);me.txtInfo_.style.display='block';me.btnReset_.style.display='inline';me.btnEnable_.src=me.ENABLE_BUTTON_IMAGE_;me.btnEnable_.title=me.ENABLE_BUTTON_TITLE_;me.updateDistance_();}else{GEvent.removeListener(me.mapClickHandle_);me.txtInfo_.style.display='none';me.btnReset_.style.display='none';me.btnEnable_.src=me.DISABLE_BUTTON_IMAGE_;me.btnEnable_.title=me.DISABLE_BUTTON_TITLE_;}}/***事件处理函数:当用户单击地图时,要在该位置添加一个标记*/GRulerControl.prototype.onMapClick_=function(marker,latlng){varme=this.rulerControl_;//如果用户单击的是标记,不再这里处理if(marker)return;//创建标记,并添加到链表中varnewMarker=newGMarker(latlng,{draggable:true});varpos=me.tail_.prev_;newMarker.prev_=pos;newMarker.next_=pos.next_;pos.next_.prev_=newMarker;pos.next_=newMarker;//为标记添加事件处理函数:拖拽标记时要更新连接线段和距离GEvent.addListener(newMarker,'dragend',function(){me.map_.closeInfoWindow();me.updateSegments_(newMarker);me.updateDistance_();});//为标记添加事件处理函数:单击标记时要显示信息窗口GEvent.addListener(newMarker,'click',function(){newMarker.openInfoWindow(me.createInfoWindow_(newMarker));});//将创建的标记添加到地图中me.map_.addOverlay(newMarker);if(newMarker.prev_!=me.head_){//如果这不是第一个标记,则创建连接到上一个标记的线段,并显示在地图中varsegment=[newMarker.prev_.getLatLng(),latlng];newMarker.segPrev_=newGPolyline(segment);newMarker.prev_.segNext_=newMarker.segPrev_;me.map_.addOverlay(newMarker.segPrev_);//更新距离显示me.updateDistance_();}}/***统计总距离,并显示在网页中*/GRulerControl.prototype.updateDistance_=function(){varme=this;varlen=me.getDistance();//结果显示在网页中me.txtInfo_.innerHTML=me.formatDistance_(len);}/***遍历链表,统计总距离*/GRulerControl.prototype.getDistance=function(){varme=this;varlen=0;//周游链表,累计相邻两个标记间的距离for(varm=me.head_;m!=me.tail_;m=m.next_){if(m.prev_&&m.prev_.getLatLng)len+=m.prev_.getLatLng().distanceFrom(m.getLatLng());}returnlen;}/***清除所有标记,初始化链表*/GRulerControl.prototype.reset=function(){varme=this;for(varm=me.head_.next_;m!=me.tail_;m=m.next_){me.map_.removeOverlay(m);if(m.segNext_)me.map_.removeOverlay(m.segNext_);}me.head_=newObject();me.tail_=newObject();me.head_.next_=me.tail_;me.tail_.prev_=me.head_;me.updateDistance_();}GRulerControl.prototype.updateSegments_=function(marker){varme=this;varsegment;//更新连接前驱的线段if(marker.segPrev_&&marker.prev_.getLatLng){//从地图上删除旧的线段me.map_.removeOverlay(marker.segPrev_);//根据标记的当前坐标构造新的线段,并更新链表结点的相关字段segment=[marker.prev_.getLatLng(),marker.getLatLng()];marker.segPrev_=newGPolyline(segment);marker.prev_.segNext_=marker.segPrev_;//将新线段添加到地图中me.map_.addOverlay(marker.segPrev_);}//更新连接后继的线段,与上类似if(marker.segNext_&&marker.next_.getLatLng){me.map_.removeOverlay(marker.segNext_);segment=[marker.getLatLng(),marker.next_.getLatLng()];marker.segNext_=newGPolyline(segment);marker.next_.segPrev_=marker.segNext_;me.map_.addOverlay(marker.segNext_);}}GRulerControl.prototype.createInfoWindow_=function(marker){varme=this;//为气泡提示窗口创建动态DOM对象,这里我们用DIV标签vardiv=document.createElement('div');div.style.fontSize='10.5pt';div.style.width='250px';div.appendChild(document.createTextNode(me.formatLatLng_(marker.getLatLng())));varhr=document.createElement('hr');hr.style.border='solid1px#cccccc';div.appendChild(hr);//创建“删除”按钮varlnk=document.createElement('div');lnk.innerHTML=me.DELETE_BUTTON_TITLE_;lnk.style.color='#0000cc';lnk.style.cursor='pointer';lnk.style.textDecoration='underline';//为“删除”按钮添加事件处理:调用removePoint()并重新计算距离lnk.onclick=function(){me.map_.closeInfoWindow();me.removePoint_(marker);me.updateDistance_();};div.appendChild(lnk);//当用户关闭信息窗口时Google地图API会自动释放该对象returndiv;}GRulerControl.prototype.removePoint_=function(marker){varme=this;//先从地图上删除该标记me.map_.removeOverlay(marker);//对于中间结点,还要把它的前驱和后继用线段连接起来if(marker.prev_.getLatLng&&marker.next_.getLatLng){varsegment=[marker.prev_.getLatLng(),marker.next_.getLatLng()];varpolyline=newGPolyline(segment);marker.prev_.segNext_=polyline;marker.next_.segPrev_=polyline;me.map_.addOverlay(polyline);}marker.prev_.next_=marker.next_;marker.next_.prev_=marker.prev_;if(marker.segPrev_)me.map_.removeOverlay(marker.segPrev_);if(marker.segNext_)me.map_.removeOverlay(marker.segNext_);}
解决方案
解决方案二:
提示有未结束的字符串常量这个肯定是你html或JS代码有未结束的表示符。GRulerControl未定义应该是你这个方法new的时候出错了
解决方案三:
还望楼上的lin_lujian详尽指点。