收藏 分销(赏)

传递坐标到Google地图.doc

上传人:仙人****88 文档编号:8469259 上传时间:2025-02-14 格式:DOC 页数:3 大小:37KB 下载积分:10 金币
下载 相关 举报
传递坐标到Google地图.doc_第1页
第1页 / 共3页
传递坐标到Google地图.doc_第2页
第2页 / 共3页


点击查看更多>>
资源描述
在本节中,你将继续在前一节的基础上构造。对AndroidLBS活动的主要修改就是传递坐标到Google地图中。你将使用Google地图来显示用户的当前位置。在main.xml文件中的唯一修改指出就是为MpaView增加一个布局。在目前版本的Android SDK中,MapView被建立为一个类View。可能在将来的版本中MapView会相当于这个布局。 <view class="com.google.android.maps.MapView" android:id="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 完成后的main.xml文件应当像这样: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= androidrientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/gpsButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Where Am I" /> <LinearLayout xmlns:android= android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/latLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Latitude: " /> <TextView android:id="@+id/latText" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android= android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/lngLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longitude: " /> <TextView android:id="@+id/lngText" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <view class="com.google.android.maps.MapView" android:id="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> 因为在这个活动中嵌入MapView,你需要改变类的定义。现在,主要类扩展了活动。但是要正确的使用Google MapView,你必须扩展MapActivity。因此,你需要输入MapActivity包装并且替换在头部的Activity 包装。 输入下列包装: import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.Point; import com.google.android.maps.MapController Point包装将被用于保留point的值,它就是展示地图坐标的,而MapController将你的point置于地图中央。这两个包装在使用MapView时非常的关键。 现在准备增加建立地图并传递坐标的代码。首先,设置一个一个MapView,并且从main.xml文件中把它赋值到布局: MapView myMap = (MapView) findViewById(R.id.myMap); 下一步,设置一个Point并且把从GPS检索的数值赋值给latPoint和IngPoint: Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue()); 现在,可以创建MapController了,它将被用于移动Google地图来定位你定义的Point。从MapView使用getController()方法在定制的地图中建立一个控制器: MapController myMapController = myMap.getController(); 唯一剩下的工作就是使用控制器来移动地图到你的位置(要让地图更容易辨认,把zoom设定为9): myMapController.centerMapTo(myLocation, false); myMapController.zoomTo(9); 你刚才所写的所有代码就是从活动中利用Google地图。完整的类应当像这样: package android_programmers_guide.AndroidLBS; import android.os.Bundle; import android.location.LocationManager; import android.view.View; import android.widget.TextView; import android.content.Context; import android.widget.Button; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.Point; import com.google.android.maps.MapController; public class AndroidLBS extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); final Button gpsButton = (Button) findViewById(R.id.gpsButton); gpsButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){ LoadProviders(); }}); } public void LoadProviders(){ TextView latText = (TextView) findViewById(R.id.latText); TextView lngText = (TextView) findViewById(R.id.lngText); LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Double latPoint = myManager.getCurrentLocation("gps").getLatitude()*1E6; Double lngPoint = myManager.getCurrentLocation("gps").getLongitude()*1E6; latText.setText(latPoint.toString()); lngText.setText(lngPoint.toString()); MapView myMap = (MapView) findViewById(R.id.myMap); Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue()); MapController myMapController = myMap.getController(); myMapController.centerMapTo(myLocation, false); myMapController.zoomTo(9); } } 在模拟器中运行活动。活动应当打开一个空白的地图。点击“Where Am I”按钮,应当会看到地图聚焦并且放大到旧金山。看看下图就会知道地图会如何出现(略)。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服