[java]
android 中,常用到的地图有百度地图、google地图、高德地图,由于项目中用到了高德,所有来做个总结。
接下来我们看看高德在android中的使用步骤:
1:相关的准备工作>>包括jar包的引入,相关权限的设置,mapview的布局
,在android的manifest.xml中加入如下的这些所需权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
//高德所需要的唯一key[需要你在高德官网申请即可]
<meta-data android:name="com.amap.api.v2.apikey" android:value="0274d320d6e750dc88a8c10b1f69f5be" />
在 main.xml中放置我们要显示的map
[java]
<com.amap.mapapi.map.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
2:在MainActivity中获取map,并且做相应的初始化设置
myMapView = (MapView) findViewById( R.id.mapview );
[java]
private void initMapParameters()
{
myMapView.setBuiltInZoomControls( true );
myGestureDetector = new GestureDetector( this );
myMapView.setOnTouchListener( new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
myTapTime = new Date().getTime();
if (Math.abs( event.getEventTime() - myTapTime ) < 500)
{
if (Math.hypot( event.getX() - myTouch_x, event.getY() - myTouch_y ) < 100)
{
return true;
}
}
myTapTime = event.getEventTime();
myTouch_x = event.getX();
myTouch_y = event.getY();
}
return myGestureDetector.onTouchEvent( event );
}
} );
myController = myMapView.getController();
}
以上代码主要是完成map的初始化,同时将map的触发操作交给我们的GestureDetector来处理
3: 接下来我们自定义一个view用来显示地图上的标注信息【地图上自定义气泡组件】如图:
它不是我们的overlay,它是我们手动打到map上我们自定义的view,用来一些额外的操作,暂且我们命名为:widget_pop.xml相关的布局文件比较简单,与我们平常的布局没有任何区别;然后是我们自定义的widgetView类【主要完成在map上的展示:不是map的overlay】,关键代码如下:
[java]
private void initContentView()
{
myFrameLayout_Root = (FrameLayout) ((LayoutInflater) myContext.getSystemService( "layout_inflater" )).inflate( R.layout.widget_mappopview, null );
this.addView( myFrameLayout_Root );
myLinearLayout_Overlay_Small = (LinearLayout) findViewById( R.id.linear_pop_small );
myLinearLayout_Overlay_Small.setVisibility( View.VISIBLE );
myLinearLayout_Overlay_Big = (LinearLayout) findViewById( R.id.linear_pop_big );
android.view.ViewGroup.LayoutParams myLayoutParams = myLinearLayout_Overlay_Big.getLayoutParams();
if (LiftApplication.ScreenHeight == 0 | LiftApplication.ScreenWidth == 0)
{
AndroidSystemUtils.getScreenInfo( myActivity );
}
myLayoutParams.width = (int) (LiftApplication.ScreenWidth * 0.8);
myLayoutParams.height = LiftApplication.ScreenHeight / 3;
myLinearLayout_Overlay_Big.setLayoutParams( myLayoutParams );
myLinearLayout_Overlay_Big.setVisibility( View.GONE );
myTextView_GPSTime = (TextView) findViewById( R.id.text_gpstime );
myTextView_Province = (TextView) findViewById( R.id.text_location_province );
myTextView_VehicleNumber = (TextView) findViewById( R.id.text_vehiclenumber );
myTextView_VehicleNumber_Small = (TextView) findViewById( R.id.text_vehiclenumber_small );
myButton_VehicleInfo = (Button) findViewById( R.id.btn_vehicledetail );
myLinearLayout_CurrentInfo = (LinearLayout) findViewById( R.id.linear_pop_currentinfo );
myLinearLayout_TraceInfo = (LinearLayout) findViewById( R.id.linear_pop_trace );
myLinearLayout_StatisticsInfo = (LinearLayout) findViewById( R.id.linear_pop_statistics );
}
以上代码主要完成了用我们自己定义的布局来生成一个组合组件,展示到map上
补充:移动开发 , Android ,