Microsoft Developer Network >
Forums Home
>
Windows Live Developer Forums Forums
>
Bing Maps: Map Control Development
>
how to find location using latlong value?
how to find location using latlong value?
- hi everybody,
I am showing the live vehicle on map. I got latlong value of that vehicle from database and then i show the vehicle on the latlong position but i also want to show the address of that latlong value. how can i do that?
i try with the example show in the link
http://dev.mapindia.live.com/sdk/
example name "Reverse find".
Here they show the address on click event but i am passing the latlong value of vehicle to that function and store the address value in variable. I am calling this function in for loop. so i got the last location address but i want the address of every vehicle location .
Can anybody give me the clue for this?
Answers
- Here is a complete example of how to make recursive calls to the FindLocations method:
<html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script> <script> var map = null; var count = 0; var points = [new VELatLong(40.67751362708503,-73.94880294799804),<br/> new VELatLong(40.71824948660362,-74.00047302246095),new VELatLong(40.766891615198205,-73.98588180541993)]; var results = []; function GetMap() { map = new VEMap('myMap'); map.LoadMap(); } function FindLocation() { if(count < points.length) { map.FindLocations(points[count], GetResults); count++; } else { resursiveCallsDone(); } } function GetResults(locations) { if(locations != null) { results.push(locations[0].Name); } else { results.push(""); } FindLocation(); } function resursiveCallsDone() { var outputText = ""; for(var i=0;i<points.length;i++) { outputText += "Latitude: " + points[i].Latitude + " Longitude: " + <br/> points[i].Longitude + " Location Name: " + results[i] + "<br/>"; } document.getElementById("results").innerHTML = outputText; } </script> </head> <body onload="GetMap();"> <div id='myMap' style="position:relative; width:800px; height:600px;"></div><br/> <input type="button" value="Do Reverse Geocode" onclick="FindLocation()"/><br/> <div id="results"></div> </body> </html>
Windows Live Developer MVP - http://rbrundritt.spaces.live.com- Proposed As Answer byRichard_BrundrittMVP, ModeratorSaturday, October 31, 2009 2:34 PM
- Marked As Answer byGuest11 Wednesday, November 04, 2009 8:50 AM
All Replies
- What you can do is make recursive calls to the VEMap.FindLocation method to do a reverse geocoding of a latlong to get the address. An example of a recursive call (using the VEMap.Find method) can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!739.entry
Windows Live Developer MVP - http://rbrundritt.spaces.live.com Hi Richart actually i don't want to pass the address argument as shown in the example show in the link
http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!739.entry
I want to access the address of the latlong where my vehicle is seen in map. LatLong is the position of my vehicle.
the code is as follows:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Multiple Buddies..</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script type="text/javascript" language="JavaScript" src="./date-picker.js"></script> <script type="text/javascript" src="http://dev.mapindia.live.com/mapcontrol/mapcontrol.ashx?v=6.2"></script> <script type="text/javascript"> var map = null; var locations = null; var pixel = null; var clickEvent = null; var LL = null; var locate=null; var val=null; var dttimearr=null ; function showMultiBuddies() { map = new VEMap('myMap'); map.LoadMap(); map.SetZoomLevel(4) // map.PanToLatLong(new VELatLong(21.15571806829396,79.0891170501709)) var cnt=new String ("<%=CNT %>"); if (cnt>0) { val=new String("<%=str_latLonlive1%>"); var names =new String ("<%=str_names%>"); var namesArr=new Array (); namesArr =names.split(","); var dttime =new String("<%=dt_time %>"); dttimearr =new Array (); dttimearr=dttime.split(","); //var areas=new String ("<%=str_Locations %>"); var pts=new Array(); pts= val.split(","); var trace=new Array(); var tracePts=""; var j=0; for(var i=0; i<=dttimearr.length-1; i++) { debugger ; getLocation(pts[j],pts[j+1]); addImage(pts[j],pts[j+1],locate,namesArr[i],dttimearr[i]); trace.push(new VELatLong(parseFloat(pts[j]), parseFloat(pts[j+1]))); j=j+2; } // var locationsArr =new Array (); // locationsArr =locate.split(","); // map.SetZoomLevel(12); // map.PanToLatLong(new VELatLong(pts[j-2],pts[j-1])); map.SetMapView(trace); } } function getLocation(x,y) { var latLong = new VELatLong(x,y); debugger ; pixel = map.LatLongToPixel(latLong); x=pixel.x; y=pixel.y; pixel=new VEPixel(x,y); LL = map.PixelToLatLong(pixel); map.FindLocations(LL, GetResults); } function GetResults(locations) { debugger ; //var s="Results for " + LL.Latitude + ", " + LL.Longitude + ": "; if(locations != null) { locate+=locations[0].Name & ","; } else { locate+=""; } alert(locate); } </script> <style type="text/css"> .style1 { width: 100%; } </style> </head> <body onload ="showMultiBuddies()" style ="background-color:#D3E4FF"> <form id="form1" runat="server"> <table class="style1"> <tr> <td> <div id='myMap' style="position: relative; width: 971px; height: 600px; top: 3px; left: 2px; border:solid 2px black"> </div> </td> </tr> </table> </form> </body> </html>i highlighted the function with underline where i am getting problem. here in "getLocation " function i am passing the latlong value of the vehicle position. in getLocation function i am using the map.FindLocations method here i passed the latlong value and here it call GetResults function. And in GetResults funtion i am getting the address of the vehicle position which i am storing in the "locate" variable.
But the problem is after calling getLocation the control executes the next line
"addImage(pts[j],pts[j+1],locate,namesArr[i],dttimearr[i]);"
and execute the function addImage. And i am passing the value of "locate" variable to this function but what is happening is the GetResult function is executed after completing the for loop and there is no value in the "locate" variable when i call the addImage function.
What is problem here? I want the value in the locate variable before executing the addImage function.- I'm aware that the example I gave you was for the Find Method. The purpose of the example is the use of recursion instead of a loop. The for loop is where your issue is. What's happening if your for loop is going through and making calls to the getLocation function and before the location information is received the loop has already gone through and started making a second call. What you will have to do is make an array of the latlong values you want to do a reverse geocode on, then you want to make recursive calls to the VEMap.FindLocation method. Here is a code block example:
var counter = 0; var points = [new VELatLong(43,-100),new VELatLong(43,-101),new VELatLong(44,-100),]new VELatLong(45,-100); var results = []; function FindLocation() { map.FindLocations(points[count], GetResults); } function GetResults(location) { results.push(location); count++; if(count < points.length) { FindLocation(); } }
Windows Live Developer MVP - http://rbrundritt.spaces.live.com- Edited byRichard_BrundrittMVP, ModeratorFriday, October 30, 2009 11:53 AM
- Here is an example that is closer to what you have in your code:
//Points and counter are global variables //trace is an array of VELatLong's of locations to be reverse Geocoded points = trace; counter = 0; GetLocations(); function GetLocations() { if(counter < points.length) { map.FindLocations(points[counter], GetResults); counter++; } else { //This gets called when all locations have finished being reverse geocoded. } } function GetResults(locations) { if(locations != null) { locate+=locations[0].Name & ","; } else { locate+=""; } GetLocation(points); }
Windows Live Developer MVP - http://rbrundritt.spaces.live.com - Now i change my code in this way:
for(var i=0; i<=dttimearr.length-1; i++) { trace.push(new VELatLong(parseFloat(pts[j]), parseFloat(pts[j+1]))); } getLocation(); for(var i=0; i<=dttimearr.length-1; i++) { addImage(pts[j],pts[j+1],results(i),namesArr[i],dttimearr[i]); j=j+2; } function getLocation() { debugger ; map.FindLocations(trace[count], GetResults); } function GetResults(locations) { debugger ; results.push(locations); if(count < results.length) { getLocation(); count++; } }but still i am getting the values of locations after executing
addImage(pts[j],pts[j+1],results(i),namesArr[i],dttimearr[i]);
i got the value of locations after executing all for loop.
The control goto the function GetResults after executing all for loop.
I want the value of locations before executing for loop.
Plz correct me if i make any error in code - Here is a complete example of how to make recursive calls to the FindLocations method:
<html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script> <script> var map = null; var count = 0; var points = [new VELatLong(40.67751362708503,-73.94880294799804),<br/> new VELatLong(40.71824948660362,-74.00047302246095),new VELatLong(40.766891615198205,-73.98588180541993)]; var results = []; function GetMap() { map = new VEMap('myMap'); map.LoadMap(); } function FindLocation() { if(count < points.length) { map.FindLocations(points[count], GetResults); count++; } else { resursiveCallsDone(); } } function GetResults(locations) { if(locations != null) { results.push(locations[0].Name); } else { results.push(""); } FindLocation(); } function resursiveCallsDone() { var outputText = ""; for(var i=0;i<points.length;i++) { outputText += "Latitude: " + points[i].Latitude + " Longitude: " + <br/> points[i].Longitude + " Location Name: " + results[i] + "<br/>"; } document.getElementById("results").innerHTML = outputText; } </script> </head> <body onload="GetMap();"> <div id='myMap' style="position:relative; width:800px; height:600px;"></div><br/> <input type="button" value="Do Reverse Geocode" onclick="FindLocation()"/><br/> <div id="results"></div> </body> </html>
Windows Live Developer MVP - http://rbrundritt.spaces.live.com- Proposed As Answer byRichard_BrundrittMVP, ModeratorSaturday, October 31, 2009 2:34 PM
- Marked As Answer byGuest11 Wednesday, November 04, 2009 8:50 AM

