您的位置:首页 > 其它

SuperMap超图——使用GPS定位自己

2016-01-23 15:38 316 查看

定位自己

1.//使用GPS定位自己

privatevoid initLocationListener() {

gpsLocationManager= (LocationManager)TabMainActivity.tabMain.

getSystemService(Context.LOCATION_SERVICE);

lastLocation= gpsLocationManager.

getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(gpsLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,

MINTIME,MININSTANCE, locationListener);

}else{

Toast.makeText(TabMainActivity.tabMain,"需要GPS定位", Toast.LENGTH_LONG)

.show();

}

}

2.自定义GPS定位自己的监听

//GPS定位自己监听

privatefinal LocationListener locationListener = new LocationListener() {

@Override

publicvoid onStatusChanged(String provider, int status, Bundle extras) {

}

@Override

publicvoid onProviderEnabled(String provider) {

}

@Override

publicvoid onProviderDisabled(String provider) {

}

@Override

publicvoid onLocationChanged(Location location) {

lastLocation= location;

updateLocation();

}

};

3.//更新本地经纬度数据

privatevoid updateLocation() {

Point2DcurLocation = new Point2D();

if(lastLocation == null) {

return;

}

curLocation.setX(lastLocation.getLongitude());

curLocation.setY(lastLocation.getLatitude());

//判断是否在地图中间

if(isLocInMap(curLocation)) {

addCallOutBall(curLocation);

}else {

addCallOutArrow(curLocation);

}

}

4.判断是否定位在显示的地图中

//定位自己是否在地图内

privateboolean isLocInMap(Point2D point2d) {

Rectangle2DrcMap = mMap.getViewBounds();

if(point2d.getX() < rcMap.getLeft()|| point2d.getX() > rcMap.getRight()) {

returnfalse;

}

if(point2d.getY() < rcMap.getBottom()|| point2d.getY() > rcMap.getTop()) {

returnfalse;

}

returntrue;

}

5.//添加自己定位标注

privatevoid addCallOutBall(Point2D loction) {

//根据name属性删除callout

mMapView.removeCallOut("loc");

CallOutcallout = new CallOut(TabMainActivity.tabMain);

callout.setStyle(CalloutAlignment.CENTER);

callout.setCustomize(true);

callout.setLocation(loction.getX(),loction.getY());

ImageViewimage = new ImageView(TabMainActivity.tabMain);

image.setBackgroundResource(R.drawable.ball);

callout.setContentView(image);

//添加带name属性的callout

mMapView.addCallout(callout,"loc");

}

6.//添加自己不在地图内的标注

privatevoid addCallOutArrow(Point2D pntLoc) {

Point2DpntMapCenter = mMap.getCenter();

Rectangle2DrcMapViewBounds = mMap.getViewBounds();

doubleuserX = (pntLoc.getX() - pntMapCenter.getX())

/rcMapViewBounds.getWidth() * screenWidth;

doubleuserY = (pntLoc.getY() - pntMapCenter.getY())

/rcMapViewBounds.getHeight() * screenHeight;

PointmarginBounds = new Point(screenWidth - SCREEN_MAERGIN,

screenHeight- SCREEN_MAERGIN);

PointarrowPosition = new Point(SCREEN_MAERGIN, SCREEN_MAERGIN);

doublearrowRotation = 0;

if(userX == 0) {

arrowPosition.setX(screenWidth/ 2);

if(userY < 0) {

arrowPosition.setX(marginBounds.getY());

arrowRotation= PI;

}else {

arrowPosition.setX(marginBounds.getX());

arrowRotation= 0;

}

rotateArrow(arrowPosition,arrowRotation);

return;

}

if(userY == 0) {

arrowPosition.setY(screenHeight/ 2);

if(userX > 0) {

arrowPosition.setX(marginBounds.getX());

arrowRotation= HALF_PI;

}else {

arrowRotation= PI + HALF_PI;

}

rotateArrow(arrowPosition,arrowRotation);

return;

}

doubleangle = Math.atan(userY / userX);

doubledCorner = Math.atan(screenHeight / screenWidth);

intballX;

intballY;

if(userY > 0) {

if(userX > 0) {// 右上

arrowRotation= HALF_PI - angle;

if(angle < dCorner) {// 小于45度,设定X,计算Y

arrowPosition.setX(marginBounds.getX());

ballY= (int) ((screenHeight - screenWidth

*Math.tan(angle)) / 2);

arrowPosition.setY(ballY);

}else {// 大于45度,设定Y,计算X

arrowPosition.setY(SCREEN_MAERGIN);

ballX= (int) ((screenWidth + screenHeight

/Math.tan(angle)) / 2);

arrowPosition.setX(ballX);

}

}else { // 左上

arrowRotation= PI + HALF_PI - angle;

if(angle > -dCorner) {// 小于45度,设定X,计算Y

arrowPosition.setX(SCREEN_MAERGIN);

ballY= (int) (0.5 * (screenHeight + screenWidth

*Math.tan(angle)));

arrowPosition.setY(ballY);

}else {// 大于45度,设定Y,计算X

arrowPosition.setY(SCREEN_MAERGIN);

ballX= (int) (0.5 * (screenWidth + screenHeight

/Math.tan(angle)));

arrowPosition.setX(ballX);

}

}

}else if (userY < 0) {

if(userX > 0) {// 右下

arrowRotation= HALF_PI - angle;

if(angle > -dCorner) {// 小于45度,设定X,计算Y

arrowPosition.setX(marginBounds.getX());

ballY= (int) ((screenHeight - screenWidth

*Math.tan(angle)) / 2);

arrowPosition.setY(ballY);

}else {// 大于45度,设定Y,计算X

arrowPosition.setY(marginBounds.getY());

ballX= (int) ((screenWidth - screenHeight

/Math.tan(angle)) / 2);

arrowPosition.setX(ballX);

}

}else if (userX < 0) { // 左下

arrowRotation= PI + HALF_PI - angle;

if(angle < dCorner) {// 小于45度,设定X,计算Y

arrowPosition.setX(SCREEN_MAERGIN);

ballY= (int) ((screenWidth * Math.tan(angle) + screenHeight) / 2);

arrowPosition.setY(ballY);

}else {// 大于45度,设定Y,计算X

arrowPosition.setY(marginBounds.getY());

ballX= (int) ((screenWidth - screenHeight

/Math.tan(angle)) / 2);

arrowPosition.setX(ballX);

}

}

}

if(arrowPosition.getX() > marginBounds.getX()) {

arrowPosition.setX(marginBounds.getX());

}else if (arrowPosition.getX() < SCREEN_MAERGIN) {

arrowPosition.setX(SCREEN_MAERGIN);

}

if(arrowPosition.getY() > marginBounds.getY()) {

arrowPosition.setY(marginBounds.getY());

}else if (arrowPosition.getY() < SCREEN_MAERGIN) {

arrowPosition.setY(SCREEN_MAERGIN);

}

rotateArrow(arrowPosition,arrowRotation);

}

//旋转箭头

@SuppressWarnings("deprecation")

privatevoid rotateArrow(Point point, double angle) {

mMapView.removeAllCallOut();

ImageViewimageArrow = new ImageView(TabMainActivity.tabMain);

Bitmapbitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.arrow);

floatfrotation = (float) (angle / PI * 180);

//创建操作图片是用的matrix对象

Matrixmatrix = new Matrix();

//缩放图片

floatscale = (float) 1.5;

matrix.postScale(scale,scale);

//旋转图片动作

matrix.postRotate(frotation);

//创建新图片

BitmapresizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,

bitmap.getWidth(),bitmap.getHeight(), matrix, true);

BitmapDrawablebmd = new BitmapDrawable(resizedBitmap);

imageArrow.setAdjustViewBounds(true);

imageArrow.setImageDrawable(bmd);

Point2Dpoint2d = mMap.pixelToMap(point);

CallOutcallout = new CallOut(TabMainActivity.tabMain);

callout.setStyle(CalloutAlignment.CENTER);

callout.setCustomize(true);

callout.setLocation(point2d.getX(),point2d.getY());

ImageViewimage = new ImageView(TabMainActivity.tabMain);

image.setImageDrawable(bmd);

callout.setContentView(image);

mMapView.addCallout(callout);

}

7.当活动销毁时:

publicvoid onDestroy() {

super.onDestroy();

mMapControl.dispose();

mWorkspace.save();

mWorkspace.close();

mInfo.dispose();

mWorkspace.dispose();

gpsLocationManager.removeUpdates(locationListener);

}

@Override

publicvoid onResume() {

super.onResume();

gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,

MINTIME,MININSTANCE, locationListener);

}

@Override

publicvoid onPause() {

super.onPause();

//取消注册监听

gpsLocationManager.removeUpdates(locationListener);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: