コード例 #1
0
  private void initBaiduMap() {
    mapView = (MapView) findViewById(R.id.bmapView);
    baiduMap = mapView.getMap();
    baiduMap.setMaxAndMinZoomLevel(18, 13);
    IntentFilter iFilter = new IntentFilter();
    iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
    iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
    receiver = new BaiduReceiver();
    registerReceiver(receiver, iFilter);

    geoCoder = GeoCoder.newInstance();
    geoCoder.setOnGetGeoCodeResultListener(this);

    Intent intent = getIntent();
    intentType = intent.getStringExtra(TYPE);
    initActionBar(R.string.chat_position);
    if (intentType.equals(TYPE_SELECT)) {
      // 选择发送位置
      // 开启定位图层
      baiduMap.setMyLocationEnabled(true);
      baiduMap.setMyLocationConfigeration(
          new MyLocationConfigeration(MyLocationConfigeration.LocationMode.NORMAL, true, null));
      // 定位初始化
      locClient = new LocationClient(this);
      locClient.registerLocationListener(myListener);
      LocationClientOption option = new LocationClientOption();
      option.setProdName("avosim");
      option.setOpenGps(true);
      option.setCoorType("bd09ll");
      option.setScanSpan(1000);
      option.setOpenGps(true);
      option.setIsNeedAddress(true);
      option.setIgnoreKillProcess(true);
      locClient.setLocOption(option);
      locClient.start();
      if (locClient != null && locClient.isStarted()) {
        locClient.requestLocation();
      }
      if (lastLocation != null) {
        // 显示在地图上
        LatLng ll = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
        baiduMap.animateMapStatus(u);
      }
    } else {
      Bundle b = intent.getExtras();
      LatLng latlng = new LatLng(b.getDouble(LATITUDE), b.getDouble(LONGITUDE)); // 维度在前,经度在后
      baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latlng));
      OverlayOptions ooA = new MarkerOptions().position(latlng).icon(descriptor).zIndex(9);
      baiduMap.addOverlay(ooA);
    }
  }
コード例 #2
0
  @Override
  public MapView createViewInstance(ThemedReactContext context) {
    reactContext = context;
    mView = new MapView(context);
    mView.onCreate(null);
    mView.onResume();
    map = mView.getMap();

    if (map == null) {
      sendMapError("Map is null", "map_null");
    } else {
      map.getUiSettings().setMyLocationButtonEnabled(true);
      map.setMyLocationEnabled(true);

      // We need to be sure to disable location-tracking when app enters background, in-case some
      // other module
      // has acquired a wake-lock and is controlling location-updates, otherwise, location-manager
      // will be left
      // updating location constantly, killing the battery, even though some other location-mgmt
      // module may
      // desire to shut-down location-services.
      LifecycleEventListener listener =
          new LifecycleEventListener() {
            @Override
            public void onHostResume() {
              map.setMyLocationEnabled(true);
            }

            @Override
            public void onHostPause() {
              map.setMyLocationEnabled(false);
            }

            @Override
            public void onHostDestroy() {}
          };

      context.addLifecycleEventListener(listener);

      try {
        MapsInitializer.initialize(context.getApplicationContext());
        map.setOnCameraChangeListener(getCameraChangeListener());
        map.setOnMarkerClickListener(getMarkerClickListener());
        // add location button click listener
        map.setOnMyLocationButtonClickListener(
            new GoogleMap.OnMyLocationButtonClickListener() {
              @Override
              public boolean onMyLocationButtonClick() {
                CameraPosition position = map.getCameraPosition();
                mlastZoom = (int) position.zoom;
                return false;
              }
            });
      } catch (Exception e) {
        e.printStackTrace();
        sendMapError("Map initialize error", "map_init_error");
      }
    }

    return mView;
  }