地图练习 博客分类: android相关 AndroidGoogleOS编程XML
程序员文章站
2024-03-05 08:16:00
...
今天练习了一下地图的编程,继承MapActivity类和Overlay类
在配置文件中添加
在Activity的OnCreate方法中设置MapView的各个属性。是否可以获得单击事件(setClickable()方法),设置地图缩放尺度(setBuiltInZoomControls(true)),设置地图的视图模式。有三种:1.街道视图2。卫星视图3。一般地图。方法分别是mapview.setStreetView();mapview.setSatelite();mapview.setTraffic()
对地图的操作是通过对一个MapController对象的操作,该对象是通过MapView.getController()方法获取的。在使地图显示某一个地点时,则是MapController.animateTo()方法,参数是一个GeoPoint类型,经度和维度的一个组合。个人感觉类似于坐标值,并且可以通过MapController.setZoom来设置放大的倍数,其中数值越大,地图的详细细节????。GeoCoder类是处理地理编码的一个类,根据输入的地点可以获取一个和此地点相关的Address类的集合。
List<Address> addresses=geoCodeer.getFromLLocationName();方法,参数有两个,一个是输入的地点,另一个是获取的地点的个数(集合的大小?)。
也可为地图设置一个图标图层,通过继承OverLay类。在此类中重写draw()方法。canvas paint 画图的“工具”,canvas.drawBitmap();canvas.drawText();.左上坐标,根据映射将地理经纬度转换的屏幕像素坐标来确定。不要忘记把你创建的图层通过map.getOverlay()(获取一个图层集合)的add方法添加到地图上。
最后不要忘了在列表中添加访问intnet的权限(<uses-permission android:name="android:permission.Internet"/>),并且为应用添加类库(<uses-library android:name="com.google.android.maps"/>),否则地图不会出来的。
例子代码1(提供用户输入查询地点的activity):
例子代码2(显示地图的MapActivity):
配置文件1:
配置文件2:
列表为:
只是同一个程序结果在1.5的上面和在2.2的上面的运行结果不一样,
原因还没找到。
在配置文件中添加
<com.google.android.maps.MapView android:id="@+id/mapview_mymap_display" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0NFa8R5kt6KmenQdcxhItm2rcaSZaNhOe3WZQTw" />
在Activity的OnCreate方法中设置MapView的各个属性。是否可以获得单击事件(setClickable()方法),设置地图缩放尺度(setBuiltInZoomControls(true)),设置地图的视图模式。有三种:1.街道视图2。卫星视图3。一般地图。方法分别是mapview.setStreetView();mapview.setSatelite();mapview.setTraffic()
对地图的操作是通过对一个MapController对象的操作,该对象是通过MapView.getController()方法获取的。在使地图显示某一个地点时,则是MapController.animateTo()方法,参数是一个GeoPoint类型,经度和维度的一个组合。个人感觉类似于坐标值,并且可以通过MapController.setZoom来设置放大的倍数,其中数值越大,地图的详细细节????。GeoCoder类是处理地理编码的一个类,根据输入的地点可以获取一个和此地点相关的Address类的集合。
List<Address> addresses=geoCodeer.getFromLLocationName();方法,参数有两个,一个是输入的地点,另一个是获取的地点的个数(集合的大小?)。
也可为地图设置一个图标图层,通过继承OverLay类。在此类中重写draw()方法。canvas paint 画图的“工具”,canvas.drawBitmap();canvas.drawText();.左上坐标,根据映射将地理经纬度转换的屏幕像素坐标来确定。不要忘记把你创建的图层通过map.getOverlay()(获取一个图层集合)的add方法添加到地图上。
最后不要忘了在列表中添加访问intnet的权限(<uses-permission android:name="android:permission.Internet"/>),并且为应用添加类库(<uses-library android:name="com.google.android.maps"/>),否则地图不会出来的。
例子代码1(提供用户输入查询地点的activity):
package com.sdut.edu.zlm; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Map extends Activity { EditText etinput; Button btquery; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListeners(); } private void setListeners() { // TODO Auto-generated method stub btquery.setOnClickListener(query); } private void findViews() { // TODO Auto-generated method stub etinput=(EditText)findViewById(R.id.edittext_main_inputaddress); btquery=(Button)findViewById(R.id.button_main_query); } Button.OnClickListener query=new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Bundle bundle=new Bundle(); Log.d("Map_query",etinput.getText().toString()); bundle.putString("address",etinput.getText().toString()); Intent intent=new Intent(); intent.setClass(Map.this,MyMap.class); intent.putExtras(bundle); startActivity(intent); } }; }
例子代码2(显示地图的MapActivity):
package com.sdut.edu.zlm; import java.util.List; import java.util.Locale; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.util.Log; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; public class MyMap extends MapActivity{//程序列表中要添加联网的权限还要加一个类库 MapView mapview; private MapController mapcontroller; private GeoPoint geopoint; protected String addressname; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.mymap); //用于显示地图上的一个ViewGroup mapview=(MapView)findViewById(R.id.mapview_mymap_display); Bundle bundle=getIntent().getExtras(); Log.d("MyMap_Oncreate_bundle",bundle+""); addressname=bundle.getString("address"); Log.d("MyMap_oncreate",addressname); // findViews(); //使得这个view可以获得点击事件 mapview.setClickable(true); //是否可以设置自动缩放设置 mapview.setBuiltInZoomControls(true); //获取控制缩放的操作对象 mapcontroller=mapview.getController(); //通过系统默认区域设置进行地图的定位 Geocoder geocoder=new Geocoder(this); mapview.setTraffic(true); try { List<Address> addresses=geocoder.getFromLocationName(addressname,2); Log.d("MyMap_oncreate_addressname3",addressname); geopoint = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); MyOverlay myoverlay=new MyOverlay(); mapview.getOverlays().add(myoverlay); mapcontroller.setZoom(20); mapcontroller.animateTo(geopoint); } catch(Exception e) { e.printStackTrace(); } } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } class MyOverlay extends Overlay { @Override public boolean draw(Canvas canvas, MapView mapview, boolean shadow, long when) { // TODO Auto-generated method stub Paint paint=new Paint(); Point screenPoint=new Point(); mapview.getProjection().toPixels(geopoint, screenPoint);//??经纬度坐标和屏幕像素坐标的一个映射 //并且这个映射可以把地理上的经纬度转换在屏幕上的像素点 Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.flag1); canvas.drawBitmap(bitmap,screenPoint.x,screenPoint.y, paint); canvas.drawText(addressname,screenPoint.x,screenPoint.y, paint); return super.draw(canvas, mapview, shadow, when); } } }
配置文件1:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/edittext_main_inputaddress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="西直门"></EditText> <Button android:text="查询" android:id="@+id/button_main_query" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout>
配置文件2:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView android:id="@+id/mapview_mymap_display" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0NFa8R5kt6KmenQdcxhItm2rcaSZaNhOe3WZQTw" /> </LinearLayout>
列表为:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sdut.edu.zlm" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps"/> <activity android:name=".Map" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MyMap"/> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.perssion.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
只是同一个程序结果在1.5的上面和在2.2的上面的运行结果不一样,
原因还没找到。
上一篇: 深入解析Java中的内部类