通过googlemap地图实现定位,以及卫星和地图(二)
main
<?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"
>
<view class="com.google.android.maps.MapView"
android:id="@+id/MapView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0r3qSWTsLVP_0VfUDmBjpARWtm3hRsTPQgDrGCg"
/>
</LinearLayout>
1,完成上次googlemap地图实现定位,以及卫星和地图的步骤。
2,创建一个主界面,界面内包含4个Menu菜单,以及googlemap地图,地图上面实现经纬度定位,以及街景,卫星视图的转换,以及经纬度的定位。代码如下
MobileMap
1 public class MobileMap extends MapActivity
2 {
3 private MapView mMapView;
4 //控制地图
5 private MapController mMapController;
6 //地址和经纬度转换获取
7 private Geocoder mGeocoder;
8 //实现图层
9 private LocationOverlay mLocationOverlay;
10 private LocationManager mlocationManager;
11 private Location mLocation;
12
13 private static final int Search = Menu.FIRST;
14 private static final int SelectCity = Menu.FIRST + 1;
15 private static final int ViewMode = Menu.FIRST + 2;
16 private static final int Exit = Menu.FIRST + 3;
17
18
19 public void onCreate(Bundle savedInstanceState)
20 {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.main);
23 init();
24
25 }
26
27
28 protected boolean isRouteDisplayed()
29 {
30 return false;
31 }
32
33 public void init()
34 {
35 mMapView=(MapView)findViewById(R.id.MapView01);
36 //取得MapController实例,控制地图
37 mMapController=mMapView.getController();
38
39 mlocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
40
41 mMapView.setEnabled(true);
42 mMapView.setClickable(true);
43 //设置显示模式(卫星和街景)
44 mMapView.setTraffic(false);
45 mMapView.setSatellite(false);
46 //mMapView.setStreetView(true);
47 //设置缩放
48 mMapView.setBuiltInZoomControls(true);
49 //设置地图等级
50 mMapController.setZoom(12);
51 //设置连接模式为GPS
52 String provider=LocationManager.GPS_PROVIDER;
53
54 //下面为基站模式和网络模式
55 /* Criteria criteria =new Criteria();
56 //经度要求
57 criteria.setAccuracy(Criteria.ACCURACY_FINE);
58 criteria.setAltitudeRequired(false);
59 criteria.setBearingRequired(false);
60 criteria.setCostAllowed(false);
61 criteria.setPowerRequirement(Criteria.POWER_LOW);
62 //取得效果最好的criteria
63 String provider=mlocationManager.getBestProvider(criteria, true);*/
64 //得到坐标相关的信息
65 mLocation=mlocationManager.getLastKnownLocation(provider);
66 //生成一个图层对象
67 mLocationOverlay=new LocationOverlay(this);
68 //获取图层链表
69 List<Overlay> overlays=mMapView.getOverlays();
70 overlays.add(mLocationOverlay);
71
72 mGeocoder = new Geocoder(this,Locale.getDefault());
73 //自定方法负责更新定位
74 updateLocation(mLocation);
75
76 mlocationManager.requestLocationUpdates(provider, 3000, 0,mLocationListener);
77
78 }
79
80 //更新定位
81 public void updateLocation(Location location)
82 {
83 if ( location == null )
84 {
85 return;
86 }
87 mLocationOverlay.setLocation(location);
88 //获取经度和纬度
89 Double geoLat=location.getLatitude()*1E6;
90 Double geoLng=location.getLongitude()*1E6;
91 //将其转换为int型
92 GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
93 //定位到指定坐标
94 mMapController.animateTo(point);
95
96 }
97 public MapController getMapController()
98 {
99 return mMapController;
100 }
101 //设置监听
102 private final LocationListener mLocationListener=new LocationListener()
103 {
104 public void onLocationChanged(Location location)
105 {
106 updateLocation(location);
107 }
108 public void onProviderDisabled(String provider){}
109 public void onProviderEnabled(String provider){}
110 public void onStatusChanged(String provider,int status,Bundle extras){}
111 };
112 //设置菜单
113 public boolean onCreateOptionsMenu(Menu menu)
114 {
115 super.onCreateOptionsMenu(menu);
116 menu.add(0, Search, Menu.NONE, "搜索地点").setIcon(R.drawable.search);
117 menu.add(0, SelectCity, Menu.NONE, "选择城市").setIcon(R.drawable.selectcity);
118 menu.add(0, ViewMode, Menu.NONE, "地图模式").setIcon(R.drawable.viewmode);
119 menu.add(0, Exit, Menu.NONE, "退出").setIcon(R.drawable.exit);
120 return true;
121 }
122 //为菜单设置监听
123 public boolean onOptionsItemSelected(MenuItem item)
124 {
125 super.onOptionsItemSelected(item);
126 switch (item.getItemId())
127 {
128 case Search:
129 searchCity();
130 return true;
131 case SelectCity:
132 selectCity();
133 return true;
134 case ViewMode:
135 selectViewMode();
136 return true;
137 case Exit:
138 this.finish();
139 return true;
140 }
141 return true;
142 }
143
144 //选择城市
145 public void selectCity()
146 {
147 OnClickListener listener = new DialogInterface.OnClickListener() {
148 public void onClick(DialogInterface dialog, int which){
149 Double geoLat=ConstData.cityCode[which][0]*1E6;
150 Double geoLng=ConstData.cityCode[which][1]*1E6;
151
152 mLocation.setLatitude(ConstData.cityCode[which][0]);
153 mLocation.setLongitude(ConstData.cityCode[which][1]);
154 mLocationOverlay.setLocation(mLocation);
155 GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
156 //定位到指定坐标
157 mMapController.animateTo(point);
158
159 }
160 };
161
162 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ConstData.city);
163 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
164
165 new AlertDialog.Builder(MobileMap.this)
166 .setTitle("请选择城市")
167 .setAdapter(adapter, listener)
168 .show();
169 }
170 //选择地图模式
171 public void selectViewMode()
172 {
173 OnClickListener listener = new DialogInterface.OnClickListener() {
174 public void onClick(DialogInterface dialog, int which)
175 {
176 switch ( which )
177 {
178 case 0:
179 mMapView.setTraffic(false);
180 mMapView.setSatellite(false);
181 //mMapView.setStreetView(true);
182 break;
183 case 1:
184 mMapView.setSatellite(false);
185 //mMapView.setStreetView(false);
186 mMapView.setTraffic(true);
187 break;
188 case 2:
189 //mMapView.setStreetView(false);
190 mMapView.setTraffic(false);
191 mMapView.setSatellite(true);
192 break;
193 }
194
195 }
196 };
197
198 String[] menu={"街景模式","交通流量","卫星地图"};
199 new AlertDialog.Builder(MobileMap.this)
200 .setTitle("请选择地图模式")
201 .setItems(menu, listener)
202 .show();
203 }
204 //搜索城市
205 public void searchCity()
206 {
207 //自定义一个带输入的对话框由TextView和EditText构成
208 final LayoutInflater factory = LayoutInflater.from(MobileMap.this);
209 final View dialogview = factory.inflate(R.layout.dialog, null);
210 //设置TextView的提示信息
211 ((TextView) dialogview.findViewById(R.id.TextView01)).setText("搜索地图");
212 //设置EditText输入框初始值
213 ((EditText) dialogview.findViewById(R.id.EditText01)).setText("请输入要查找的地址...");
214
215 Builder builder = new Builder(MobileMap.this);
216 builder.setTitle("请输入地名");
217 builder.setView(dialogview);
218 builder.setPositiveButton(android.R.string.ok,
219 new AlertDialog.OnClickListener() {
220 public void onClick(DialogInterface dialog, int which) {
221 //点击确定之后
222 String value = ((EditText) dialogview.findViewById(R.id.EditText01)).getText().toString().trim();
223 if ( value != "" )
224 {
225 searchName(value);
226 }
227 }
228 });
229 builder.setNegativeButton(android.R.string.cancel,
230 new DialogInterface.OnClickListener() {
231 public void onClick(DialogInterface dialog, int which) {
232 dialog.cancel();
233 }
234 });
235 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
236 public void onCancel(DialogInterface dialog) {
237 dialog.cancel();
238 }
239 });
240 builder.show();
241 }
242
243 public void searchName(String nameStr)
244 {
245 try
246 {
247 List<Address> addresses = mGeocoder.getFromLocationName(nameStr, 1);
248 if (addresses.size() != 0)
249 {
250 Address address = addresses.get(0);
251 GeoPoint geoPoint = new GeoPoint((int) (address.getLatitude() * 1E6), (int) (address.getLongitude() * 1E6));
252
253 mLocation.setLatitude(address.getLatitude());
254 mLocation.setLongitude(address.getLongitude());
255 mLocationOverlay.setLocation(mLocation);
256
257 mMapController.animateTo(geoPoint);
258 }
259 }
260 catch (IOException e){}
261 }
262
263
264 }
2.第二实现定位图层,包含定位图层的美工修饰,以及定位功能方法的实现。代码如下:
LocationOverlay
1 public class LocationOverlay extends Overlay
2 {
3 private Location mLocation;
4 private MobileMap mMobileMap;
5 private String mAddresses;
6 //获取传入的mobileMap
7 public LocationOverlay(MobileMap mobileMap)
8 {
9 mMobileMap = mobileMap;
10 }
11 //获取location
12 public void setLocation(Location location)
13 {
14 mLocation = location;
15 mAddresses = getAddresses();
16 }
17
18 public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
19 {
20 super.draw(canvas, mapView, shadow);
21 if(mLocation!=null){
22 Paint paint = new Paint();
23 Point scPoint = new Point();
24 GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
25 //Projection代表了MapView的视窗坐标与经纬度坐标的映射关系
26 //把经纬度转换成视窗坐标
27 mapView.getProjection().toPixels(tmpGeoPoint, scPoint);
28 //设置画笔宽度
29 paint.setStrokeWidth(1);
30 //设置画笔的颜色和透明度
31 paint.setARGB(255, 255, 0, 0);
32 //样式
33 paint.setStyle(Paint.Style.STROKE);
34 //消除锯齿
35 paint.setFlags(Paint.ANTI_ALIAS_FLAG);
36 //设置字体大小
37 paint.setTextSize(16);
38
39 Bitmap bmp = BitmapFactory.decodeResource(mMobileMap.getResources(), R.drawable.mark);
40 //在画布上画图
41 canvas.drawBitmap(bmp, scPoint.x-bmp.getWidth()/2, scPoint.y-bmp.getHeight(), paint);
42 //在画布上写字
43 canvas.drawText(mAddresses, scPoint.x-paint.measureText(mAddresses)/2, scPoint.y, paint);
44 return true;}
45 else
46 {return false;}
47 }
48 //负责经纬度定位,以及定位字符显示
49 public String getAddresses()
50 {
51 String addressString="没有找到地址";
52 Geocoder gc=new Geocoder(mMobileMap,Locale.getDefault());
53 try
54 {
55 List<Address> addresses=gc.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(),1);
56 StringBuilder sb=new StringBuilder();
57 if(addresses.size()>0)
58 {
59 Address address = addresses.get(0);
60 sb.append("地址:");
61 for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
62 {
63 sb.append(address.getAddressLine(i));
64 }
65 addressString=sb.toString();
66 }
67 }catch(IOException e){}
68
69 return addressString;
70 }
71 }
3.第三布局方式


浙公网安备 33010602011771号