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

原创 2016年01月27日 10:20:56

融云,发送位置,腾讯地图改为高德地图。修改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())
                    .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);
    }
}


融云&高德地图-实现地理位置发送

效果预览 Send: Click LocationMessage: 实现 1:注册高德地图开发者账号,创建应用、获取高德地图的 appkey 2: jar 包建议直接从 融云 ...

高德地图02---高德地图添加覆盖物

1 添加MarkOption标记“` MarkerOptions mark = new MarkerOptions(); LatLng lat=new LatLng(39.908127, 11...

关于使用高德地图没有找到LocationManagerProxy类的问题

由于高德地图API升级的太快,导致之前能用的几乎都没了,这个LocationManagerProxy类在Android_Location_V1.3.0还能用,好不容找到,这个,现在分享一下这个包...

融云&百度地图-实现地理位置发送

注册百度地图开发者账号,获取百度地图的appkey,下载相关的地图jar包 实现融云地理位置点击事件,我是在会话页面设置的监听事件,具体方法可以参考融云的demo public class Conve...

基于百度地图实现融云 sdk 2.8.0+ 发送地理位置

融云 sdk 2.8.0+ 内置的高德地图的发送位置。但是百度地图在地图市场占有率也是相当高的。为了满足使用百度地图的开发者,本篇文档对如何在 sdk 2.8.0 以上实现百度地图发送地理位置消息做讲...

百度、搜狗、高德、腾讯等地图完美处理偏移

工具:WOLFMAP 地图下载器   做GIS行业的朋友应该清楚,在我朝的发布的地图必须经过加密后才能使用,也就是俗称的火星坐标,有的地图还在它的基础上进行二次加密。给我们使用造成一定的困扰。 ...
  • wolfmap
  • wolfmap
  • 2016年07月15日 11:11
  • 1452

h5判断百度、高德、腾讯地图客户端是否安装及调起客户端导航

百度定位: 引入:             /**              *开始定位              */             function getLocation() { ...

主流地图 坐标系转换,百度、腾讯、高德等

在我国,为了国家安全,电子地图不可以使用地球坐标系WGS84,必须经过偏转。面前主流的几款地图都有其对应的坐标系。 例如 高德、腾讯、图灵、阿里地图等都是 GCJ-02坐标系(也称火星坐标系) 而百度...

腾讯 百度 高德 Google 地图导航 实现

最近做地图导航功能,顾实现这四种地图导航功能,代码如下: 高德地图代码: public static void openGaoDeMap(Context context, String nam...

基于高德地图实现融云位置共享功能

基于高德地图实现融云位置共享功能(Android篇)效果预览: 开发准备:1: 登录 融云开发者账号,提交 server 平台工单申请开通 实时位置共享功能。工单回复开通成功后、 2 小时生效。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:融云,发送位置,腾讯地图改为高德地图,SOSOLocationActivity 核心类修改
举报原因:
原因补充:

(最多只允许输入30个字)