zoukankan      html  css  js  c++  java
  • 百度地图Overlay

    package com.lzz.asfp;

     

    public class PetroleumServerActivity extends Activity implements OnClickListener {

    /**
    * MapView 是地图主控件
    */

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private Marker mMarker;
    private Marker mMarker1;
    private InfoWindow mInfoWindow;
    int position;
    private TextView myOrder;
    private LinearLayout back;

    double d1;
    double d2;

    private String lat = "", lng = "";

    String imageUrl;
    // 初始化全局 bitmap 信息,不用时及时 recycle
    BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.drawable.jiayouicon);

    BitmapDescriptor bdGround = BitmapDescriptorFactory.fromResource(R.drawable.dingwei);

    private HashMap<String, String> map;
    private List<OilStationInfo> info = new ArrayList<>(); // 所有Overlay点位信息

    private int image = 0;

    @SuppressLint("HandlerLeak")
    Handler h = new Handler() {
    public void handleMessage(android.os.Message msg) {
    if (msg.what == 1) {
    initOverLay();
    }
    };
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_petroleum_server);
    LocationApplication application = (LocationApplication) getApplication();
    application.addOilActivity(this);

    // 百度定位转换为GPS定位
    BDLocation location = new BDLocation();
    location.setLatitude(Double.valueOf(NetWorkUtils.getLocLatitude(this)));
    location.setLongitude(Double.valueOf(NetWorkUtils.getLocLongitude(this)));

    BDLocation location1 = GlobalTool.BAIDU_to_WGS84(location);
    lat = location1.getLatitude() + "";
    lng = location1.getLongitude() + "";

    back = (LinearLayout) findViewById(R.id.back);
    initData();
    mMapView = (MapView) findViewById(R.id.bmapView);
    mBaiduMap = mMapView.getMap();
    myOrder = (TextView) findViewById(R.id.my_order);
    myOrder.setOnClickListener(this);
    back.setOnClickListener(this);
    // 构造一个地图函数的msu对象,设置对象的缩放等级问14.0,最后设置地图状态。
    // MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
    // mBaiduMap.setMapStatus(msu);
    LatLng l = new LatLng(Double.valueOf(NetWorkUtils.getLocLatitude(this)),
    Double.valueOf(NetWorkUtils.getLocLongitude(this)));
    MapStatus mStatus = new MapStatus.Builder().target(l).build();
    MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mStatus);
    // 改变地图状态
    mBaiduMap.setMapStatus(mMapStatusUpdate);

    }

    public void initOverLay() {

    //循环加入每一条信息

    // add ground overlay
    d1 = Double.valueOf(NetWorkUtils.getLocLatitude(this));
    d2 = Double.valueOf(NetWorkUtils.getLocLongitude(this));
    LatLng lll = new LatLng(d1, d2);
    OverlayOptions ooAa = new MarkerOptions().position(lll).icon(bdGround).zIndex(9).draggable(true);
    mMarker1 = (Marker) (mBaiduMap.addOverlay(ooAa));
    Bundle bundle1 = new Bundle();
    bundle1.putString("store_id", "1");
    bundle1.putString("store_name", "");
    mMarker1.setExtraInfo(bundle1);

    if (null != info) {
    if (image < info.size()) {
    new Thread(new Runnable() {
    @Override
    public void run() {
    while (image < info.size()) {
    // 进行网络加载图片(放到地图上的图标)
    if (!"".equals(info.get(image).getLogo_img_url())
    && null != info.get(image).getLogo_img_url()) {
    imageUrl = info.get(image).getLogo_img_url();

    HttpGet httpRequest = new HttpGet(imageUrl);
    // 取得HttpClient 对象
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
    // 请求httpClient ,取得HttpRestponse
    HttpResponse httpResponse = httpclient.execute(httpRequest);
    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

    // 取得相关信息 取得HttpEntiy
    HttpEntity httpEntity = httpResponse.getEntity();
    // 获得一个输入流
    InputStream is = httpEntity.getContent();
    System.out.println(is.available());
    System.out.println("Get, Yes!");
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    // Bitmap bitmap3 = drawBg4Bitmap(getResources().getColor(R.color.green),bitmap2);
    // Bitmap bitmap = BitmapFillet.fillet(bitmap3, 30, 3);

    if (is != null) {
    bdA = BitmapDescriptorFactory.fromBitmap(bitmap);
    if (image < info.size()) {

    LatLng desLatLng1 = new LatLng(Double.valueOf(info.get(image).getLat()),
    Double.valueOf(info.get(image).getLng()));
    // //GPS转百度
    // 将GPS设备采集的原始GPS坐标转换成百度坐标
    CoordinateConverter converter = new CoordinateConverter();
    converter.from(CoordType.GPS);
    // sourceLatLng待转换坐标
    converter.coord(desLatLng1);
    LatLng desLatLng = converter.convert();
    double la = desLatLng.latitude;
    double ln = desLatLng.longitude;
    LatLng ll = new LatLng(la, ln);
    OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA)
    .zIndex(9).draggable(true);
    mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
    Bundle bundle = new Bundle();
    bundle.putString("store_id", info.get(image).getStore_id());
    bundle.putString("store_name", info.get(image).getStore_name());
    mMarker.setExtraInfo(bundle);
    image++;
    } else {
    return;
    }
    }

    is.close();
    // iv.setImageBitmap(bitmap);
    }

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    bdA = BitmapDescriptorFactory.fromResource(R.drawable.moren);
    if (image < info.size()) {

    LatLng desLatLng1 = new LatLng(Double.valueOf(info.get(image).getLat()),
    Double.valueOf(info.get(image).getLng()));
    // //GPS转百度
    // 将GPS设备采集的原始GPS坐标转换成百度坐标
    CoordinateConverter converter = new CoordinateConverter();
    converter.from(CoordType.GPS);
    // sourceLatLng待转换坐标
    converter.coord(desLatLng1);
    LatLng desLatLng = converter.convert();
    double la = desLatLng.latitude;
    double ln = desLatLng.longitude;
    LatLng ll = new LatLng(la, ln);
    OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA).zIndex(9)
    .draggable(true);
    mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
    Bundle bundle = new Bundle();
    bundle.putString("store_id", info.get(image).getStore_id());
    bundle.putString("store_name", info.get(image).getStore_name());
    mMarker.setExtraInfo(bundle);
    image++;
    } else {
    return;
    }
    }

    } else {
    bdA = BitmapDescriptorFactory.fromResource(R.drawable.moren);
    if (image < info.size()) {

    LatLng desLatLng1 = new LatLng(Double.valueOf(info.get(image).getLat()),
    Double.valueOf(info.get(image).getLng()));
    // //GPS转百度
    // 将GPS设备采集的原始GPS坐标转换成百度坐标
    CoordinateConverter converter = new CoordinateConverter();
    converter.from(CoordType.GPS);
    // sourceLatLng待转换坐标
    converter.coord(desLatLng1);
    LatLng desLatLng = converter.convert();
    double la = desLatLng.latitude;
    double ln = desLatLng.longitude;
    LatLng ll = new LatLng(la, ln);
    OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA).zIndex(9)
    .draggable(true);
    mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
    Bundle bundle = new Bundle();
    bundle.putString("store_id", info.get(image).getStore_id());
    bundle.putString("store_name", info.get(image).getStore_name());
    mMarker.setExtraInfo(bundle);
    image++;
    } else {
    return;
    }
    }
    }

    }

    }).start();
    }

    }

    mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    @SuppressLint("InflateParams")
    public boolean onMarkerClick(final Marker marker) {

    // 在这里进行循环遍历
    for (int i = 0; i < info.size(); i++) {

    //通过上面Marker添加的ExtraInfo区分点击的是哪一个marker点,传递不同的信息
    if (marker.getExtraInfo().getString("store_id").equals(info.get(i).getStore_id())) {
    View view = getLayoutInflater().inflate(R.layout.oil_infowindow_layout, null);
    TextView name = (TextView) view.findViewById(R.id.store_name);
    TextView address = (TextView) view.findViewById(R.id.store_address);
    OnInfoWindowClickListener listener = null;

    position = i;
    name.setText(info.get(position).getStore_name());
    address.setText(info.get(position).getAddress());
    listener = new OnInfoWindowClickListener() {
    public void onInfoWindowClick() {
    // LatLng all = marker.getPosition();
    // LatLng llNew = new LatLng(ll.latitude +
    // 0.005,
    // ll.longitude + 0.005);
    // marker.setPosition(llNew);
    Intent intent = new Intent(PetroleumServerActivity.this,
    PetroleumDetailsActivity.class);
    intent.putExtra("store_id", info.get(position).getStore_id());
    intent.putExtra("address", info.get(position).getAddress());
    intent.putExtra("store_name", info.get(position).getStore_name());
    intent.putExtra("lng", info.get(position).getLng());
    intent.putExtra("lat", info.get(position).getLat());
    intent.putExtra("img", info.get(position).getLogo_img_url());
    startActivity(intent);
    mBaiduMap.hideInfoWindow();
    }
    };
    LatLng ll = marker.getPosition();
    mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(view), ll, -65, listener);
    mBaiduMap.showInfoWindow(mInfoWindow);
    new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    Thread.sleep(4000);
    mBaiduMap.hideInfoWindow();

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }).start();
    break;
    } else if ("1".equals(marker.getExtraInfo().getString("store_id"))) {
    View view = getLayoutInflater().inflate(R.layout.myposition_infowindow_layout, null);
    OnInfoWindowClickListener listener = null;
    // button.setText(info.get(position).getStore_name()+" "+info.get(position).getAddress());
    // button.setTextSize(15);
    // button.setGravity(Gravity.CENTER_VERTICAL);
    listener = new OnInfoWindowClickListener() {
    public void onInfoWindowClick() {
    mBaiduMap.hideInfoWindow();
    }
    };
    LatLng ll = marker.getPosition();
    mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(view), ll, -52, listener);
    mBaiduMap.showInfoWindow(mInfoWindow);
    new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    Thread.sleep(3000);
    mBaiduMap.hideInfoWindow();

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    });
    break;
    }
    }
    return true;
    }
    });
    }

    /**
    * 清除所有Overlay
    *
    * @param view
    */
    public void clearOverlay(View view) {
    mBaiduMap.clear();
    }

    /**
    * 重新添加Overlay
    *
    * @param view
    */
    public void resetOverlay(View view) {
    clearOverlay(null);
    // initOverlay();
    }

    @Override
    protected void onPause() {
    // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
    mMapView.onPause();
    super.onPause();
    }

    @Override
    protected void onResume() {
    // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
    mMapView.onResume();
    super.onResume();
    }

    @Override
    protected void onDestroy() {
    // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
    mMapView.onDestroy();
    super.onDestroy();
    // 回收 bitmap 资源
    bdA.recycle();
    }

    //网络请求拿到所有的Overlay信息

    public void initData() {

    map = new HashMap<>();
    map.put("lng", lng);
    map.put("lat", lat);
    if (NetWorkUtils.isAvailable(PetroleumServerActivity.this)) {
    String url = UrlPath.GASSTATIONINFO;
    DownData.instance().downDataPost(url, map, new onDownListener() {
    @Override
    public void getValue(boolean isSuccess, String value) {
    if (isSuccess && value != null) {
    Gson gson = new Gson();
    OilInfoVo oilInfo = gson.fromJson(value, OilInfoVo.class);
    if ("0".equals(oilInfo.getErrorCode())) {
    info = oilInfo.getData();
    h.sendEmptyMessage(1);
    } else {
    Toast.makeText(PetroleumServerActivity.this, "系统处理异常!", Toast.LENGTH_SHORT).show();
    }
    } else {
    Toast.makeText(PetroleumServerActivity.this, "网络异常!", Toast.LENGTH_SHORT).show();
    }
    }
    });
    } else {
    Toast.makeText(PetroleumServerActivity.this, "网络异常,请稍后重试!", Toast.LENGTH_SHORT).show();
    }
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.my_order:
    Intent intent = new Intent(PetroleumServerActivity.this, MyOilOrderListActivity.class);
    startActivity(intent);
    break;
    case R.id.back:
    finish();
    break;
    default:
    break;
    }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    // mBaiduMap.hideInfoWindow();
    return super.onTouchEvent(event);
    }


    //bitmap绘制背景颜色
    public static Bitmap drawBg4Bitmap(int color, Bitmap orginBitmap) {
    Paint paint = new Paint();
    paint.setColor(color);
    Bitmap bitmap = Bitmap.createBitmap(orginBitmap.getWidth(),
    orginBitmap.getHeight(), orginBitmap.getConfig());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawRect(0, 0, orginBitmap.getWidth(), orginBitmap.getHeight(), paint);
    canvas.drawBitmap(orginBitmap, 0, 0, paint);
    return bitmap;
    }

    //bitmap设置圆角

    }

  • 相关阅读:
    黑产江湖
    FW/IDS/IPS/WAF等安全设备部署方式及优缺点
    SOAPA来临,SIEM时代终结?
    美国爱因斯坦计划4
    零基础如何学好安卓开发
    协同办公系统能为企业带来怎样的影响?
    阿里腾讯开撕,钉钉的广告打到腾讯的地盘了
    bug管理工具为开发者工作带来哪些改变?
    开发人员必备的几款bug管理工具
    教你玩转产品管理系统iClap(PC端功能篇)
  • 原文地址:https://www.cnblogs.com/fuyinshan/p/6733388.html
Copyright © 2011-2022 走看看