最近做了地图的功能,用了超图,就是简单实现显示当前位置。
前期
首先导入超图的sdk,我用的是iclient_android_7.1.0.jar。
权限及布局
网络访问权限:<uses-permission android:name="android.permission.INTERNET" />
GPS定位权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
这是精确定位的权限,如果用到网络定位或基站定位,加入<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.supermap.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true" />
<Button
android:id="@+id/btn_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dip"
android:background="@drawable/location_marker" />
</RelativeLayout>
MapView是图层,在图层上再放置覆盖物,如定位的按钮btn_location。
显示当前位置
主界面:
mapView = (MapView) findViewById(R.id.mapview);
LayerView layerView = new LayerView(this);
layerView.setURL(Constant.URL_MAP);
mapView.setBuiltInZoomControls(true);
mapView.addLayer(layerView);
final Drawable drawableBlue = getResources().getDrawable(R.drawable.blue_pin);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
LocationUtil.initLocation(mapView, drawableBlue);
}
}, 2000);
LocationUtil:
public static void initLocation(MapView view, Drawable drawable)
{
if (view == null)
{
return;
}
mapView = view;
context = view.getContext().getApplicationContext();
if (context == null)
{
return;
}
overlay = new DefaultItemizedOverlay(drawable);
if (!mapView.getOverlays().contains(overlay))
{
mapView.getOverlays().add(overlay);
}
// times 用来计数第几次定位
times = 1;
mLocationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
gpsListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 3, gpsListener);
}
private static class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
Point2D gp = null;
String title = "当前位置";
try
{
gp = new Point2D(location.getLongitude(),
location.getLatitude());
overlayItem = new OverlayItem(gp, title, title);
overlay.clear();
overlay.addItem(overlayItem);
if (times < 2)
{
mapView.getController().animateTo(gp);
mapView.getController().setZoom(mapView.getMaxZoomLevel());
}
// 重新onDraw一次
mapView.invalidate();
times++;
// Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
} catch (Exception e)
{
e.printStackTrace();
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onProviderDisabled(String arg0)
{
System.out.println(arg0);
}
@Override
public void onProviderEnabled(String arg0)
{
System.out.println(arg0);
}
@Override
public void onStatusChanged(String arg0, int status, Bundle arg2)
{
}
}
LayerView是覆盖物图层,防止覆盖物。layerView.setURL(Constant.URL_MAP),放入你的iclient地址。mapView.setBuiltInZoomControls(true);设置可放大缩小。
postDelayed,之所以要延时发送,主要是我在用的时候,发现如果地图图层出不来的话,就永远出不来了,所以认为等待2s,等待地图图层显示出来。
new Point2D(location.getLongitude(), location.getLatitude());生成地图上的二维点。
overlay.addItem(overlayItem);将覆盖物放入覆盖物图层。
mapView.getController().animateTo(gp);设置地图中心点。
mapView.getController().setZoom(mapView.getMaxZoomLevel());设置地图缩放等级。
mapView.invalidate();地图重绘。
这样就简单的实现了超图的定位显示功能,因为百度不到相关资料,所以在这儿分享一下。