Example #1
0
        protected override void OnElementChanged(ElementChangedEventArgs <Page> e)
        {
            base.OnElementChanged(e);
            myAMapPage = e.NewElement as TencentMapPage;
            layout1    = new LinearLayout(this.Context);
            this.AddView(layout1);

            mapView = new MapView(this.Context)
            {
                LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
            };
            LatLng SHANGHAI = new LatLng(31.238068, 121.501654);

            mapView.Map.SetCenter(SHANGHAI);

            var pins = myAMapPage.Pins;

            Drawable d      = Resources.GetDrawable(Resource.Drawable.red_location);
            Bitmap   bitmap = ((BitmapDrawable)d).Bitmap;
            LatLng   latLng1;

            foreach (UserTaskEntInfo pin in pins)
            {
                latLng1 = new LatLng(pin.Longitude ?? 31.238068, pin.Latitude ?? 121.501654);
                var markOption = new MarkerOptions();
                markOption.InvokeIcon(new BitmapDescriptor(bitmap));
                markOption.InvokeTitle(pin.Name);
                markOption.InvokePosition(latLng1);
                var fix = mapView.Map.AddMarker(markOption);
                fix.ShowInfoWindow();
            }
            mapView.Map.SetZoom(15);
            mapView.OnCreate(bundle);
            layout1.AddView(mapView);
        }
        private void AddMarker(GoogleMap map, double lat, double lon, int title, int snippet)
        {
            MarkerOptions options = new MarkerOptions();

            options.InvokePosition(new LatLng(lat, lon));
            options.InvokeTitle(GetString(title));
            options.InvokeSnippet(GetString(snippet));

            map.AddMarker(options);
        }
Example #3
0
        private void setUpMap()
        {
            LatLng SHANGHAI = new LatLng(31.238068, 121.501654);

            aMap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(SHANGHAI, 14));
            aMap.MapType = AMap.MapTypeNormal;

            var pins = myAMapPage.Pins;

            Drawable d      = Resources.GetDrawable(Resource.Drawable.red_location);
            Bitmap   bitmap = ((BitmapDrawable)d).Bitmap;
            LatLng   latLng1;

            foreach (UserTaskEntInfo pin in pins)
            {
                latLng1 = new LatLng(pin.Longitude ?? 31.238068, pin.Latitude ?? 121.501654);
                var markOption = new MarkerOptions();
                markOption.InvokeIcon(BitmapDescriptorFactory.FromBitmap(bitmap));
                markOption.InvokeTitle(pin.Name);
                markOption.InvokePosition(latLng1);
                var fix = aMap.AddMarker(markOption);
                fix.ShowInfoWindow();
            }

            aMap.SetLocationSource(this);                   // 设置定位监听
            aMap.UiSettings.MyLocationButtonEnabled = true; // 设置默认定位按钮是否显示
            aMap.MyLocationEnabled = true;                  // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
            //aMap.SetMyLocationType(AMap.LOCATION_TYPE_LOCATE);//设置定位的类型为跟随模式,3D地图才有;

            // 自定义系统定位小蓝点
            MyLocationStyle myLocationStyle = new MyLocationStyle();

            myLocationStyle.InvokeMyLocationIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.location_marker)); // 设置小蓝点的图标
            myLocationStyle.InvokeStrokeColor(Android.Graphics.Color.Black);                                               // 设置圆形的边框颜色
            myLocationStyle.InvokeRadiusFillColor(Android.Graphics.Color.Argb(100, 0, 0, 180));                            // 设置圆形的填充颜色
            myLocationStyle.InvokeStrokeWidth(1.0f);                                                                       // 设置圆形的边框粗细
            aMap.SetMyLocationStyle(myLocationStyle);
        }
Example #4
0
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement == null || e.OldElement != null)
            {
                return;
            }

            //e.NewElement就是承载的界面,这里就是PCL项目里面的MainPage
            //var mainPage = e.NewElement as TencentMapSamples;
            var mainPage = e.NewElement as Samples.Controls.TencentMap;

            //初始化mapView
            mapView = new MapView(this.Context);
            mapView.OnCreate(null);

            //初始化视图
            layout = new LinearLayout(this.Context);
            layout.AddView(mapView);
            this.AddView(layout);

            //这里可以比对以下我们的写法跟腾讯官网里Java写法的区别,可以看出Java里面的属性是set,get前缀,而在C#里面都被隐藏了,直接用C#惯用的属性写法来代替,而方法则还是同样的SetXXX(),GetXXX(),但是Java是camelCasing,C#用PascalCasing写法(博主非常喜欢C#写法,而很讨厌Java的写法 :-))。这些区别,都是Xamarin 里 绑定Java库的转换规则。

            #region TencentMap类
            //腾讯地图的设置是通过TencentMap类进行设置,可以控制地图的底图类型、显示范围、缩放级别、添加 / 删除marker和图形,此外对于地图的各种回调监听也是绑定到TencentMap。下面是TencentMap类的使用示例:

            //获取TencentMap实例
            TencentMap tencentMap = mapView.Map;
            //设置实时路况开启
            tencentMap.TrafficEnabled = true;
            //设置地图中心点
            tencentMap.SetCenter(new Com.Tencent.Mapsdk.Raster.Model.LatLng(30.605870, 104.068610));
            //设置缩放级别
            tencentMap.SetZoom(11);
            #endregion

            #region UiSettings类
            //UiSettings类用于设置地图的视图状态,如Logo位置设置、比例尺位置设置、地图手势开关等。下面是UiSettings类的使用示例:

            //获取UiSettings实例
            UiSettings uiSettings = mapView.UiSettings;
            //设置logo到屏幕底部中心
            uiSettings.SetLogoPosition(UiSettings.LogoPositionCenterBottom);
            //设置比例尺到屏幕右下角
            uiSettings.SetScaleViewPosition(UiSettings.ScaleviewPositionRightBottom);
            //启用缩放手势(更多的手势控制请参考开发手册)
            uiSettings.SetZoomGesturesEnabled(true);
            #endregion

            #region 使用marker
            //注意,这里要往resources/drawable/里添加一个red_location.png的图片
            var bitmap           = Resources.GetBitmap("red_location.png");
            BitmapDescriptor des = new BitmapDescriptor(bitmap);
            foreach (var item in mainPage.Options)
            {
                MarkerOptions options = new MarkerOptions();

                options.InvokeIcon(des);
                options.InvokeTitle(item.Title);
                options.Anchor(0.5f, 0.5f);
                options.InvokePosition(new LatLng(item.Lat, item.Lng));
                options.Draggable(true);
                Marker marker = mapView.AddMarker(options);
                marker.ShowInfoWindow();
            }

            #endregion
        }
Example #5
0
        private void UpdateTaxiLocation(TaxiLocation value)
        {
            if (value != null && value.Latitude.HasValue && value.Longitude.HasValue && value.VehicleNumber.HasValue())
            {
                ShowAvailableVehicles(null);

                // Update Marker and Animate it to see it move on the map
                if (_taxiLocationPin != null)
                {
                    var icon = ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue
                        ? Map.IconFactory.FromBitmap(DrawHelper.RotateImageByDegreesWithСenterCrop(Resource.Drawable.nearby_oriented_passenger, value.CompassCourse.Value))
                        : Map.IconFactory.FromBitmap(CreateTaxiBitmap());

                    AnimateMarkerOnMap(icon, _taxiLocationPin, new LatLng(value.Latitude.Value, value.Longitude.Value), value.CompassCourse, new LatLng(value.Latitude.Value, value.Longitude.Value));

                    if (_showVehicleNumber)
                    {
                        Map.SelectMarker(_taxiLocationPin.Marker);
                    }
                }

                // Create Marker the first time
                else
                {
                    try
                    {
                        var icon = ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue
                            ? Map.IconFactory.FromBitmap(DrawHelper.RotateImageByDegreesWithСenterCrop(Resource.Drawable.nearby_oriented_passenger, value.CompassCourse.Value))
                            : Map.IconFactory.FromBitmap(CreateTaxiBitmap());

                        var mapOptions = new MarkerOptions()
                                         .InvokePosition(new LatLng(value.Latitude.Value, value.Longitude.Value))
                                         .InvokeIcon(icon);

                        if (_showVehicleNumber)
                        {
                            var inflater        = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
                            var addBottomMargin = !(ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue);
                            var markerAdapter   = new CustomMarkerPopupAdapter(inflater, addBottomMargin, _resources, value.Market);
                            Map.InfoWindowAdapter = markerAdapter;
                            mapOptions.InvokeTitle(value.VehicleNumber);
                        }
                        _taxiLocationPin = mapOptions;

                        Map.AddMarker(mapOptions);

                        if (_showVehicleNumber)
                        {
                            Map.SelectMarker(_taxiLocationPin.Marker);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex);
                    }

                    _isBookingMode = true;

                    return;
                }
            }

            // Booking is now over, so we need to clean up.
            if (value == null && _taxiLocationPin != null)
            {
                _isBookingMode = false;
                Map.RemoveMarker(_taxiLocationPin.Marker);
                //_taxiLocationPin.Marker.Remove();
                _taxiLocationPin = null;
            }
        }