您的位置:首页 > 其它

融云,发送位置,腾讯地图改为高德地图,SOSOLocationActivity 核心类修改

2016-01-27 10:20 585 查看
融云,发送位置,腾讯地图改为高德地图。修改demo的SOSOLocationActivity 核心类:

高德用的是3d地图包+搜索包,2d地图的实时交通覆盖层一直有bug。

代码如下:

package com.uroad.cst;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.overlay.PoiOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.uroad.cst.common.AppContext;
import com.uroad.cst.common.BaseMapActivity;
import com.uroad.cst.common.Constants;
import com.uroad.cst.widget.h;

import java.util.ArrayList;
import java.util.List;

import io.rong.message.LocationMessage;

/**
* Created by DragonJ on 14/11/21.
*/

@SuppressLint("ClickableViewAccessibility")
public class AMAPLocationActivity extends BaseMapActivity implements
AMapLocationListener, Handler.Callback, View.OnTouchListener {

MapView mMapView;
LocationMessage mMsg;
Handler mHandler;
Handler mWorkHandler;
AMap mAmap = null;
TextView mTitle;
LatLonPoint latLonPoint;
/**
* 当前地图地址的poi
*/
private HandlerThread mHandlerThread;

private final static int RENDER_POI = 1;
private final static int SHWO_TIPS = 2;

private Context mContext;

private List<h> a = new ArrayList();

@Override
/**
*显示地图,启用内置缩放控件,并用MapController控制地图的中心点及Zoom级别
*/
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setBaseContentLayout(R.layout.de_ac_amap_map);
mContext = this;

mHandlerThread = new HandlerThread("LocationThread");
mHandlerThread.start();
mWorkHandler = new Handler(mHandlerThread.getLooper());
mHandler = new Handler(this);

initView(bundle);

if (getIntent().hasExtra("location")) {
mMsg = getIntent().getParcelableExtra("location");
}

if (mMsg == null) {
setRightBtn("确认", R.color.transparent, 55, 35);
}

if (mMsg == null) {
setTitle("发送位置");
setUpMapLocation();
mAmap.moveCamera(CameraUpdateFactory.zoomTo(16));
mMapView.setOnTouchListener(this);

} else {
setTitle("查看位置");
LatLng point = new LatLng(mMsg.getLat(), mMsg.getLng() ); // 用给定的经纬度构造一个LatLng
LatLonPoint point2 = new LatLonPoint(mMsg.getLat(), mMsg.getLng()); // 用给定的经纬度构造一个LatLng

PoiItem poiItem = new PoiItem(mMsg.getPoi(),point2,"","");

mHandler.obtainMessage(RENDER_POI, poiItem).sendToTarget();
findViewById(android.R.id.icon).setVisibility(View.GONE);
setUpPointLocation();
mAmap.moveCamera(CameraUpdateFactory.changeLatLng(point));
mAmap.moveCamera(CameraUpdateFactory.zoomTo(16));
}

}

private void initView(Bundle bundle) {
mMapView = (MapView) findViewById(android.R.id.widget_frame);
initMap(mMapView, bundle);
mTitle = (TextView) findViewById(android.R.id.title);
mAmap = mMapView.getMap();

}

@Override
public void onLocationChanged(AMapLocation amapLocation) {

if (amapLocation != null && amapLocation.getAMapException().getErrorCode() == 0) {
//获取位置信息
final Double geoLat = amapLocation.getLatitude();
final Double geoLng = amapLocation.getLongitude();
mHandler.post(new Runnable() {
@Override
public void run() {
LatLng point = new LatLng(geoLat,geoLng); // 用给定的经纬度构造一个LatLng
latLonPoint = new LatLonPoint(geoLat,geoLng);
mAmap.moveCamera(CameraUpdateFactory.changeLatLng(point));
mWorkHandler.post(new POISearchRunnable());
}
});
} else {
Toast.makeText(this, "定位失败", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onDestroy() {
int iSize = this.a.size();
h process = null;

for(int j = 0; j < iSize; ++j) {
process = (h)this.a.get(0);
if(process != null) {
process.onDestroy();
this.a.remove(0);
}
}

System.gc();

if (AppContext.getInstance().getLastLocationCallback() != null)
AppContext.getInstance().getLastLocationCallback().onFailure("失败");

AppContext.getInstance().setLastLocationCallback(null);

super.onDestroy();
}

@Override
protected void onRightClick(View v) {

if (mMsg != null) {
AppContext.getInstance().getLastLocationCallback().onSuccess(mMsg);
AppContext.getInstance().setLastLocationCallback(null);
finish();
} else {
AppContext.getInstance().getLastLocationCallback()
.onFailure("定位失败");
}

}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public boolean handleMessage(Message msg) {
if (msg.what == RENDER_POI) {
PoiItem poiItem = (PoiItem) msg.obj;
List<PoiItem> list = new ArrayList<PoiItem>();

list.add(poiItem);

mMapView.getOverlay().clear();
PoiOverlay myPoiOverlay = new PoiOverlay(mAmap,list);
myPoiOverlay.addToMap();
myPoiOverlay.zoomToSpan();

mTitle.setText(poiItem.getAdName());
mTitle.setVisibility(View.VISIBLE);

Uri uri = Uri
.parse("http://restapi.amap.com/v3/staticmap").buildUpon().appendQueryParameter("size", "340*240")
.appendQueryParameter("key", Constants.AMAP_WEB_KEY).appendQueryParameter("zoom", "16")
.appendQueryParameter("location", poiItem.getLatLonPoint().getLongitude() + ","
+ poiItem.getLatLonPoint().getLatitude())
.appendQueryParameter("markers", "mid,,"+"A:"+poiItem.getLatLonPoint().getLongitude()+ ","
+ poiItem.getLatLonPoint().getLatitude()).build();

Log.d("uri",uri.toString());

mMsg = LocationMessage.obtain(poiItem.getLatLonPoint().getLatitude(),
poiItem.getLatLonPoint().getLongitude(), poiItem.getAdName(), uri);
} else if (msg.what == SHWO_TIPS) {

PoiItem poiItem = (PoiItem) msg.obj;

mTitle.setText(poiItem.getAdName());
mTitle.setVisibility(View.VISIBLE);

Uri uri = Uri
.parse("http://restapi.amap.com/v3/staticmap").buildUpon().appendQueryParameter("size", "340*240")
.appendQueryParameter("key", Constants.AMAP_WEB_KEY).appendQueryParameter("zoom", "16")
.appendQueryParameter("location", poiItem.getLatLonPoint().getLongitude() + ","
+ poiItem.getLatLonPoint().getLatitude())

4000
.appendQueryParameter("markers", "mid,,"+"A:"+poiItem.getLatLonPoint().getLongitude()+ ","
+ poiItem.getLatLonPoint().getLatitude()).build();

Log.e("tag","-----uri---"+uri);
mMsg = LocationMessage.obtain(poiItem.getLatLonPoint().getLatitude(),
poiItem.getLatLonPoint().getLongitude(), poiItem.getAdName(), uri);

}
return false;
}

POISearchRunnable mLastSearchRunnable;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mLastSearchRunnable != null)
mWorkHandler.removeCallbacks(mLastSearchRunnable);

mTitle.setVisibility(View.INVISIBLE);
mHandler.removeMessages(RENDER_POI);

break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
mLastSearchRunnable = new POISearchRunnable();
mWorkHandler.post(new POISearchRunnable());
break;
default:
break;
}

return false;
}

private class POISearchRunnable implements Runnable {

public void run() {
try {
GeocodeSearch geocoderSearch = new GeocodeSearch(AMAPLocationActivity.this);

geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
PoiItem poiItem = new PoiItem(null,latLonPoint,"","");
poiItem.setAdName( result.getRegeocodeAddress().getFormatAddress());

if(getIntent().hasExtra("location"))
mHandler.obtainMessage(RENDER_POI, poiItem).sendToTarget();
else
mHandler.obtainMessage(SHWO_TIPS, poiItem).sendToTarget();

if (result.getRegeocodeAddress().getProvince() == null) {
// mLocationProvinceTextView.setText("null");
} else {
// mLocationProvinceTextView.setText(result.getRegeocodeAddress().getProvince());
}
// mLocationCityTextView.setText(result.getRegeocodeAddress().getCity());
// mLocationCountyTextView.setText(result.getRegeocodeAddress().getDistrict());

} else {
showShortToast("没有结果");
}
} else if (rCode == 27) {
showShortToast("网络错误");
} else if (rCode == 32) {
showShortToast("key无效");
} else {
showShortToast("错误码:"+rCode+",请联系管理员!");
}
}

@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

}
});

RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,GeocodeSearch.AMAP);
geocoderSearch.getFromLocationAsyn(query);

} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
protected void onPause() {
super.onPause();
int iSize = this.a.size();
h process = null;

for(int i = 0; i < iSize; ++i) {
process = (h)this.a.get(i);
if(process != null) {
process.onPause();
}
}

}
@Override
protected void onResume() {
super.onResume();
int iSize = this.a.size();
h process = null;

for(int i = 0; i < iSize; ++i) {
process = (h)this.a.get(i);
if(process != null) {
process.onResume();
}
}

}
@Override
protected void onRestart() {
super.onRestart();
int iSize = this.a.size();
h process = null;

for(int i = 0; i < iSize; ++i) {
process = (h)this.a.get(i);
if(process != null) {
process.onRestart();
}
}

}
@Override
protected void onStop() {
super.onStop();
int iSize = this.a.size();
h process = null;

for(int i = 0; i < iSize; ++i) {
process = (h)this.a.get(i);
if(process != null) {
process.onStop();
}
}

}

void a(h ProcessCon) {
this.a.add(ProcessCon);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IM 高德地图