Hello,
Our .MapUnit has been set to .DecimalDegree forever. Now we are looking into using Google or Bing maps, which require the .MapUnit to be set to .Meter. Any idea how difficult it would be to convert to using the different .MapUnit type? Are there easy functions in place, such that we don’t need to rework the code in every place that we draw something based on degrees instead? Am I even asking the right questions?
Thanks,
Dib
Decimal Degrees to Meters
Hi Dib,
You need to convert your shapes include extent from decimal degree to meter, you should want to use our ManagedProj4Projection for that.
ManagedProj4Projection proj4 = new ManagedProj4Projection();
proj4.InternalProjectionParametersString = ManagedProj4Projection.GetDecimalDegreesParametersString();
proj4.ExternalProjectionParametersString = ManagedProj4Projection.GetBingMapParametersString(); // It works for Bing map, for Google map please use the other one
proj4.Open();
If your used any DecimalDegree shape file, you need to assign this to your shape file feature layer, so that all the shapes in file will be rendered under new projection.
shapeFileFeatureLayer.FeatureSource.Projection = proj4;
And if you use any decimal degree point/line/area, you need convert them also:
PointShape newPointShape = proj4.ConvertToExternalProjection(pointShape) as PointShape;
LineShape newLineShape = proj4.ConvertToExternalProjection(lineShape) as LineShape;
RectangleShape currentExtent = proj4.ConvertToExternalProjection(extent) as RectangleShape;
Wish that’s helpful.
Regards,
Don
Don,
Thanks for the code. In trying it out, I am running into an error. The code is (VB.Net):
Dim proj4 As New ManagedProj4Projection
Dim windPoly4 As MultipointShape
proj4.InternalProjectionParametersString = ManagedProj4Projection.GetGoogleMapParametersString
proj4.Open()
windPoly4 = proj4.ConvertToExternalProjection(windPoly)
proj4.Close()
When it hits proj4.Open, a ‘Parameters is Null’ error is thrown. I’ve also tried this with GetDecimalDegreesParametersString instead of GetGoogleMapParametersString, but the same error is thrown. Any ideas what I might be missing?
Also, I’m trying to use a BingMapsOverlay, setting the map type to Road, and adding it as the only overlay. But when running, all the map control shows is all pink with the red X in the middle. Do I need to initialize the Bing map in some other way?
Forget the Bing part, got a key and got it working.
Hi Dib,
Are all the above questions had been solved?
Regards,
Don
No, I am still getting the ‘Parameters is null’ error when the code hits the proj4.Open line.
Hi Dib,
proj4.InternalProjectionParametersString = ManagedProj4Projection.GetGoogleMapParametersString
proj4.Open()
It looks you hadn’t set the ExternalProjectionParametersString.
I think that’s why it throw exception.
Regards,
Don
Don,
I found that myself, and have set it using both Google and Bing maps, but there error continues to be thrown. Here is the stack trace:
at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at Hurrevac2010.Hurrevac2010.ShowStreetsWithData(Object sender, EventArgs e) in C:\Users\Dib Oglesby\Documents\Visual Studio 2010\Projects\hurrevac-1-6\branches\1.6.0\Hurrevac2010\Forms\Hur2010Form.vb:line 15469
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Hurrevac2010.My.MyApplication.Main(String[] Args) in :line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Hi Dib,
I am sorry it looks from this exception information I cannot find what’s the problem here.
Please upload your test project and let me try to reproduce that.
Regards,
Don
Don,
Trying to upload a sample project, but it looks like it is too big. All I did was put a map on a form, and in the form Load, add a Bing map layer, then try to open a ManagedProj4Projection object. Here is the entire code of the project (VB.Net):
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim layerOver As New LayerOverlay
Dim bingLayer As New BingMapsLayer("Bing Map Key")
bingLayer.MapType = BingMapsMapType.Road
bingLayer.IsVisible = False
bingLayer.Name = "BingMap"
bingLayer.IsVisible = True
layerOver.Layers.Add(bingLayer.Name, bingLayer)
Me.WinformsMap1.CurrentExtent = New RectangleShape(-110, 50, -50, 0)
Me.WinformsMap1.CurrentScale = 73957193.82
Me.WinformsMap1.Overlays.Add(layerOver)
Me.WinformsMap1.Refresh()
Dim proj4 As New ManagedProj4Projection
proj4.InternalProjectionParametersString = ManagedProj4Projection.GetBingMapParametersString
proj4.Open()
proj4.Close()
End Sub
End Class
Hi Dib,
Please test the code as below, which works here:
Private Sub DisplayMap_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.winformsMap1.MapUnit = GeographyUnit.Meter
Dim layerOver As New LayerOverlay
Dim bingLayer As New BingMapsLayer(“Your Bing Map Key”)
bingLayer.MapType = BingMapsMapType.Road
bingLayer.Name = “BingMap”
bingLayer.IsVisible = True
layerOver.Layers.Add(bingLayer.Name, bingLayer)
Dim proj4 As New ManagedProj4Projection
proj4.InternalProjectionParametersString = ManagedProj4Projection.GetDecimalDegreesParametersString
proj4.ExternalProjectionParametersString = ManagedProj4Projection.GetBingMapParametersString
proj4.Open()
Dim rect As RectangleShape = New RectangleShape(-110, 50, -50, 0)
Me.winformsMap1.CurrentExtent = proj4.ConvertToExternalProjection(rect)
'Me.winformsMap1.CurrentScale = 73957193.82
Me.winformsMap1.Overlays.Add(layerOver)
Me.winformsMap1.Refresh()
End Sub
Regards,
Don
That seems to work. I’ll get into our regular code and see if it does the trick
Thanks
Hi Dib,
Any question please let us know.
Regards,
Don