PDA

View Full Version : Please help me to check the code about Adding pushpin



john453
08-06-2011, 06:16 PM
Dear all:

I am going crazy about the code as to how to add pushpins in the Mappoint Map. The code is written in VBA(Mappoint embedded in EXCEL).
The whole code is displayed as follows:


Private Sub Add_Pushpins_Click()

Dim objApp As MapPoint.Application
Dim objLoc As MapPoint.FindResults

Set objApp = CreateObject("MapPoint.Application.EU.16")
objApp.Visible = True

Set objLoc = objApp.ActiveMap.FindAddressResults(, , , , "CV4 7AL", geoCountryUnitedKingdom)

objApp.ActiveMap.AddPushpin objLoc, "Added Pushpin"

End Sub

I checked my code for a couple of times, but it is still not working. The system keep sending message" Type mismatch" for the code : "objApp.ActiveMap.AddPushpin objLoc, "Added Pushpin" "

I am really going crazy about this and I cannot figure out exactly which part went wrong .The whole program is in the attached file. Can everyone helpe me out ? I cannot thank you enough!

Eric Frost
08-07-2011, 05:11 PM
Hello!

FindAddressResults is a collection, not a location. That line you point out would actually be fine if you make a couple adjustments up above.


Private Sub Add_Pushpins_Click()

Dim objApp As MapPoint.Application
Dim objFR As MapPoint.FindResults
Dim objLoc As MapPoint.Location

Set objApp = CreateObject("MapPoint.Application.EU.16")
objApp.Visible = True

Set objFR = objApp.ActiveMap.FindAddressResults(, , , , "CV4 7AL", geoCountryUnitedKingdom)
Set objLoc = objFR(1)

objApp.ActiveMap.AddPushpin objLoc, "Added Pushpin"

End Sub

I didn't test that, but hopefully it should get you in the right direction. Also see the Help file examples.

The reason FindAddressResults is a collection instead of a single Location object like you might expect, is that geocoding is not perfect, there might be two or more possible "guesses" that the geocoding engine makes and the real result (lat/lon or location) you want may or may not actually be in that collection, it's just trying to find it... Also, the plural FindAddressResults is a clue.

Hope this helps, let us know how it goes!

Eric

PS. if you haven't seen already, the page Working With Excel and MapPoint - MP2K Magazine (http://www.mp2kmag.com/excel) contains several good Excel VBA/MapPoint examples. Another great source is the MapForums Downloads section MapForums Plus - MapForums Plus Downloads at MapForums.com (http://www.mapforums.com/mapforums-plus-downloads/) ($99 for access), all the tools in there have Excel VBA source code you can copy and use.

Eric Frost
08-07-2011, 05:14 PM
By the way, in case you were curious, yes you can save a couple lines of code by doing this --


Set objLoc = objApp.ActiveMap.FindAddressResults(, , , , "CV4 7AL", geoCountryUnitedKingdom)(1)


I just thought illustrating use of the objFr object would be more clear how things are working :-)

Eric