Android GPS开发常用方法
fmms
13年前
<span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"><strong>GPS常用方法总结 <br /> 取得LocationProvider</strong> <br /> </span> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">public</span><span style="color:black;"> </span><span style="color:#7f0055;font-weight:bold;" class="keyword">void</span><span style="color:black;"> getLocationProvider() </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">try</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> Criteria mCriteria01 = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Criteria(); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mCriteria01.setAltitudeRequired(<span style="color:#7f0055;font-weight:bold;" class="keyword">false</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mCriteria01.setBearingRequired(<span style="color:#7f0055;font-weight:bold;" class="keyword">false</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mCriteria01.setCostAllowed(<span style="color:#7f0055;font-weight:bold;" class="keyword">true</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mCriteria01.setPowerRequirement(Criteria.POWER_LOW); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> strLocationProvider = </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mLocationManager01.getBestProvider(mCriteria01, <span style="color:#7f0055;font-weight:bold;" class="keyword">true</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mLocation01 = mLocationManager01.getLastKnownLocation </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> (strLocationProvider); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span><span style="color:black;">(Exception e) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mTextView01.setText(e.toString()); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> e.printStackTrace(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> </ol> </div> <br /> <strong>获取经纬度,并返回GeoPoint对象</strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">private</span><span style="color:black;"> GeoPoint getGeoByLocation(Location location) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> GeoPoint gp = <span style="color:#7f0055;font-weight:bold;" class="keyword">null</span><span style="color:black;">; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">try</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">/* 当Location存在 */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">if</span><span style="color:black;"> (location != </span><span style="color:#7f0055;font-weight:bold;" class="keyword">null</span><span style="color:black;">) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">double</span><span style="color:black;"> geoLatitude = location.getLatitude()*1E6; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">double</span><span style="color:black;"> geoLongitude = location.getLongitude()*1E6; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> gp = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> GeoPoint((</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">) geoLatitude, (</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">) geoLongitude); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span><span style="color:black;">(Exception e) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> e.printStackTrace(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">return</span><span style="color:black;"> gp; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> </ol> </div> <br /> <strong>将经纬度转换成实际屏幕坐标</strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:black;">Point myScreenCoords = </span><span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Point(); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">GeoPoint tmpGeoPoint = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> GeoPoint((</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)(mLocation.getLatitude()*1E6),(</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)(mLocation.getLongitude()*1E6)); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords); </span></li> </ol> </div> <br /> <br /> <strong>点击MapView任意一点获得坐标</strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#646464;" class="annotation">@Override</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">public</span><span style="color:black;"> </span><span style="color:#7f0055;font-weight:bold;" class="keyword">boolean</span><span style="color:black;"> onTouchEvent(MotionEvent ev) { </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;"> actionType = ev.getAction(); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">switch</span><span style="color:black;"> (actionType) { </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">case</span><span style="color:black;"> MotionEvent.ACTION_UP: </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> Projection proj = mapView.getProjection(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> GeoPoint loc = proj.fromPixels((<span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)arg0.getX(), (</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)arg0.getY()); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> String sirina=Double.toString(loc.getLongitudeE6()/<span style="color:#c00000;" class="number">1000000</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> String dolzina=Double.toString(loc.getLatitudeE6()/<span style="color:#c00000;" class="number">1000000</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">return</span><span style="color:black;"> </span><span style="color:#7f0055;font-weight:bold;" class="keyword">false</span><span style="color:black;">; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <br /> <br /> <br /> <strong>经纬度改变来刷新地图</strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">public</span><span style="color:black;"> </span><span style="color:#7f0055;font-weight:bold;" class="keyword">void</span><span style="color:black;"> refreshMapView() </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">{ </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> GeoPoint p = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> GeoPoint((</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)(dLat* 1E6), (</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)(dLng* 1E6)); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mMapView01.displayZoomControls(<span style="color:#7f0055;font-weight:bold;" class="keyword">true</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">/* 将Map的中点移至GeoPoint */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mMapController01.animateTo(p); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mMapController01.setZoom(intZoomLevel); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <br /> <br /> <strong>根据当前的经纬度,获取相关的一些地址信息</strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">/* 创建Geocoder对象 */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">//根据地理环境来确定编码</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">//注意这个Locale是java.util.Locale包的类,获取当前系统设定的语言</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> Geocoder gc = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Geocoder </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> (EX09_05.<span style="color:#7f0055;font-weight:bold;" class="keyword">this</span><span style="color:black;">, Locale.getDefault()); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">/* 取出地理坐标经纬度 */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">double</span><span style="color:black;"> geoLatitude = (</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)gp.getLatitudeE6()/1E6; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">double</span><span style="color:black;"> geoLongitude = (</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">)gp.getLongitudeE6()/1E6; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">/* 自经纬度取得地址(可能有多行地址) */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> List<Address> lstAddress = </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> gc.getFromLocation(geoLatitude, geoLongitude, <span style="color:#c00000;" class="number">1</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> StringBuilder sb = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> StringBuilder(); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">/* 判断地址是否为多行 */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">if</span><span style="color:black;"> (lstAddress.size() > </span><span style="color:#c00000;" class="number">0</span><span style="color:black;">) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> Address adsLocation = lstAddress.get(<span style="color:#c00000;" class="number">0</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">for</span><span style="color:black;">(</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;"> i=</span><span style="color:#c00000;" class="number">0</span><span style="color:black;">;i<adsLocation.getMaxAddressLineIndex();i++) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> sb.append(adsLocation.getAddressLine(i)).append(<span style="color:blue;" class="string">"\n"</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> sb.append(adsLocation.getLocality()).append(<span style="color:blue;" class="string">"\n"</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> sb.append(adsLocation.getPostalCode()).append(<span style="color:blue;" class="string">"\n"</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> sb.append(adsLocation.getCountryName()); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> </ol> </div> <br /> <br /> <br /> <strong>根据输入地址,取得其GeoPoint对象 </strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">private</span><span style="color:black;"> GeoPoint getGeoByAddress(String strSearchAddress) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">{ </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> GeoPoint gp = <span style="color:#7f0055;font-weight:bold;" class="keyword">null</span><span style="color:black;">; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">try</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">if</span><span style="color:black;">(strSearchAddress!=</span><span style="color:blue;" class="string">""</span><span style="color:black;">) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> Geocoder mGeocoder01 = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Geocoder </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> (EX09_07.<span style="color:#7f0055;font-weight:bold;" class="keyword">this</span><span style="color:black;">, Locale.getDefault()); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> List<Address> lstAddress = mGeocoder01.getFromLocationName </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> (strSearchAddress, <span style="color:#c00000;" class="number">1</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">if</span><span style="color:black;"> (!lstAddress.isEmpty()) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> Address adsLocation = lstAddress.get(<span style="color:#c00000;" class="number">0</span><span style="color:black;">); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">double</span><span style="color:black;"> geoLatitude = adsLocation.getLatitude()*1E6; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">double</span><span style="color:black;"> geoLongitude = adsLocation.getLongitude()*1E6; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> gp = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> GeoPoint((</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">) geoLatitude, (</span><span style="color:#7f0055;font-weight:bold;" class="keyword">int</span><span style="color:black;">) geoLongitude); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span><span style="color:black;"> (Exception e) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> e.printStackTrace(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">return</span><span style="color:black;"> gp; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <br /> <br /> <strong>地图放大缩小按钮</strong> <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">/* 放大Map的Button */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mButton02 = (Button)findViewById(R.id.myButton2); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mButton02.setOnClickListener(<span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Button.OnClickListener() </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">public</span><span style="color:black;"> </span><span style="color:#7f0055;font-weight:bold;" class="keyword">void</span><span style="color:black;"> onClick(View v) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> intZoomLevel++; </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">if</span><span style="color:black;">(intZoomLevel>mMapView01.getMaxZoomLevel()) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> intZoomLevel = mMapView01.getMaxZoomLevel(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mMapController01.setZoom(intZoomLevel); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> }); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">/* 缩小Map的Button */</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mButton03 = (Button)findViewById(R.id.myButton3); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mButton03.setOnClickListener(<span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Button.OnClickListener() </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">public</span><span style="color:black;"> </span><span style="color:#7f0055;font-weight:bold;" class="keyword">void</span><span style="color:black;"> onClick(View v) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> intZoomLevel--; </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">if</span><span style="color:black;">(intZoomLevel<</span><span style="color:#c00000;" class="number">1</span><span style="color:black;">) </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> intZoomLevel = <span style="color:#c00000;" class="number">1</span><span style="color:black;">; </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> mMapController01.setZoom(intZoomLevel); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> } </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> }); </span></li> </ol> </div> <br /> <strong><span style="font-size:16px;">android location provider有两个: </span></strong> <br /> <br /> * LocationManager.GPS_PROVIDER:GPS,精度比较高,但是慢而且消耗电力,而且可能因为天气原因或者障碍物而无法获取卫星信息,另外设备可能没有GPS模块; <br /> * LocationManager.NETWORK_PROVIDER:通过网络获取定位信息,精度低,耗电少,获取信息速度较快,不依赖GPS模块。 <br /> <br /> 为了程序的通用性,希望动态选择location provider。对android通过Location API显示地址信息做了个别改动,可以看到使用了gps定位,精度较高: <br /> <br /> <img title="d38467c5-217c-3131-87e5-1f2743f5a22a.png" border="0" alt="d38467c5-217c-3131-87e5-1f2743f5a22a.png" src="https://simg.open-open.com/show/ca9301bf05dc49adea7cdcc0fc5c4eea.png" width="230" height="370" /> <br /> <br /> 这里使用到了Criteria,可根据当前设备情况自动选择哪种location provider。见 <br /> <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:black;">LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">Criteria criteria = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span><span style="color:black;"> Criteria(); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">criteria.setAccuracy(Criteria.ACCURACY_FINE);<span style="color:#008200;" class="comment">// 设置为最大精度</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">criteria.setAltitudeRequired(<span style="color:#7f0055;font-weight:bold;" class="keyword">false</span><span style="color:black;">);</span><span style="color:#008200;" class="comment">//不要求海拔信息</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">criteria.setBearingRequired(<span style="color:#7f0055;font-weight:bold;" class="keyword">false</span><span style="color:black;">);</span><span style="color:#008200;" class="comment">// 不要求方位信息</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">criteria.setCostAllowed(<span style="color:#7f0055;font-weight:bold;" class="keyword">true</span><span style="color:black;">);</span><span style="color:#008200;" class="comment">//是否允许付费</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">criteria.setPowerRequirement(Criteria.POWER_LOW);<span style="color:#008200;" class="comment">// 对电量的要求</span><span style="color:black;"> </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">location = locationManager </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> .getLastKnownLocation(locationManager.getBestProvider(criteria, <span style="color:#7f0055;font-weight:bold;" class="keyword">true</span><span style="color:black;">)); </span></span></li> </ol> </div> <br /> 原来的写法很简单: <div style="padding-bottom:1px;overflow-x:auto;overflow-y:auto;background-color:transparent;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;font-size:12px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="text-align:left;padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;color:black;font-weight:bold;padding-top:3px;" class="tools"> Java代码 </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;background-color:#ffffff;list-style-type:decimal;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:black;">LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); </span></span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">location=locationManager.getLastKnownLocation(LocationManager.NETWORK </span></li> <li><br /> </li> </ol> </div>