473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Google MAP: Can not addListener to polygons which parsed from XML

Anyone help me please.
I want anytime mousemove to one polygon, it's opacity becomes 0.3, and mouseout polygon, i'ts opacity return to 0 again. I means mousemove to show the polygon and hiding it when mouseout.
I parsed points from XML and then add polygons to the map.
But my problem that I can't addListener (mousemove) and (mouseout) for
each of polygon, so I decide to addListener to the map and check if
any polygon contains clicked point. But my map is too slow with my
algorithms. Anyone help me to fix to addListener to each of polygons?.
Thank you very much !
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. var polys = [];
  2.         var labels = [];
  3.         var htmls = [];
  4.  
  5.  
  6.       // === A method for testing if a point is inside a polygon
  7.       // === Returns true if poly contains point
  8.       // === Algorithm shamelessly stolen from http://alienryderflex.com/polygon/
  9.       GPolygon.prototype.Contains = function(point) {
  10.         var j=0;
  11.         var oddNodes = false;
  12.         var x = point.lng();
  13.         var y = point.lat();
  14.         for (var i=0; i < this.getVertexCount(); i++) {
  15.           j++;
  16.           if (j == this.getVertexCount()) {j = 0;}
  17.           if (((this.getVertex(i).lat() < y) &&
  18. (this.getVertex(j).lat() >= y))
  19.           || ((this.getVertex(j).lat() < y) &&
  20. (this.getVertex(i).lat() >= y))) {
  21.             if ( this.getVertex(i).lng() + (y -
  22. this.getVertex(i).lat())
  23.             /  (this.getVertex(j).lat()-this.getVertex(i).lat())
  24.             *  (this.getVertex(j).lng() - this.getVertex(i).lng())<x )
  25. {
  26.               oddNodes = !oddNodes
  27.             }
  28.           }
  29.         }
  30.         return oddNodes;
  31.       }
  32.  
  33.  
  34.       GEvent.addListener(map, "click", function(overlay,point) {
  35.       confirm(point);
  36.         if (!overlay) {
  37.           for (var i=0; i<polys.length; i++) {
  38.             if (polys[i].Contains(point)) {
  39.               var myHTML = "<font color=red><b>" + labels[i] + "</b></font><br /
  40. >" + htmls[i];
  41.               map.openInfoWindowHtml(point,myHTML);
  42.               //i = 999; // Jump out of loop
  43.             }
  44.           }
  45.         }
  46.       });
  47.  
  48.       GEvent.addListener(map, "mousemove", function(point) {
  49.           for (var i=0; i<polys.length; i++) {
  50.             if (polys[i].Contains(point)) {
  51.               var myPoly = polys[i];
  52.               myPoly.setFillStyle({opacity: 0.3});
  53.               cur_poly = i;
  54.               //i = 999; // Jump out of loop
  55.             }
  56.             else {
  57.                 var myPoly = polys[i];
  58.                 myPoly.setFillStyle({opacity: 0});
  59.             }
  60.           }
  61.       });
  62.  
  63.       // Read the data from companies.xml
  64.  
  65.       var request = GXmlHttp.create();
  66.       request.open("GET", "' . base_path() . $mod_path . '/
  67. companies.xml", true);
  68.       request.onreadystatechange = function() {
  69.         if (request.readyState == 4) {
  70.           var xmlDoc = GXml.parse(request.responseText);
  71.  
  72.           // ========= Now process the polylines ===========
  73.           var companies =
  74. xmlDoc.documentElement.getElementsByTagName("company");
  75.  
  76.           // read each line
  77.           for (var a = 0; a < companies.length; a++) {
  78.             // get any state attributes
  79.             var label  = companies[a].getAttribute("name");
  80.             // read each point on that line
  81.             var points = companies[a].getElementsByTagName("point");
  82.             var pts = [];
  83.             for (var i = 0; i < points.length; i++) {
  84.               pts[i] = new
  85. GLatLng(parseFloat(points[i].getAttribute("lat")),
  86.  
  87. parseFloat(points[i].getAttribute("lng")));
  88.             }
  89.             var htmlObject = companies[a].getElementsByTagName("html");
  90.             if (htmlObject.length > 0)
  91.                 var html = htmlObject[0].childNodes[0].nodeValue;
  92.             else
  93.                 var html = "";
  94.             var poly = new GPolygon(pts,"#000000",0,0,"#0000CC",0,
  95. {clickable:false});
  96.             polys.push(poly);
  97.             labels.push(label);
  98.             htmls.push(html);
  99.             map.addOverlay(poly);
  100.           }
  101.           //
  102. ================================================
  103.         }
  104.       }
  105.       request.send(null);
Nov 5 '08 #1
1 4982
acoder
16,027 Expert Mod 8TB
I think mouseover might be more efficient unless you need co-ordinates for every mouse movement. Also, you're using the click event, but you've set "clickable" to false when creating the polygon (line 95).
Nov 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jim Land | last post by:
I've got an image and an image map, and they work fine together. See it here: http://www.jim-land.com/testing/MapPage01.htm But the image map has way too many points (vertices) in its 87...
0
by: windandwaves | last post by:
Hi Folk Some of you may find this function useful. It makes it easier to create maps using Google Maps. I link it to a database of points. The main use of the function is that it creates all...
17
by: MeerkatInFrance | last post by:
There comes a time when you know you are not going to be able to work something out yourself, however hard you try. I have reached that moment. I have a master page and a slave page (or whatever...
1
by: sacksey | last post by:
Hi. I've been trying to play with this for a while now but I haven't seemed to get it to work. I have a admin controller, which is scaffolded, to input the lat and long along with info. I have a...
0
by: ryt2aamir | last post by:
Hi. I've been trying to play with this for a while now but I haven't seemed to get it to work. I have a admin controller, which is scaffolded, to input the lat and long along with info. I have a...
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
0
by: TrevRex | last post by:
Hello, I work for a non-profit in San Diego as a GIS Specialist. I have had to teach myself about some scripting to create some dynamic maps, but I am still very limited in my skills, so I have...
3
by: ziycon | last post by:
I'm not sure if anyone here would know anything about the google map js, i've tried the google maps api community with no luck. I have the below code, it loads ok, but when i click on the map it ...
5
by: Nike1984 | last post by:
I'm fairly new to Javascript and it's more of a guessing game for me... I'm trying to build an app for Google Maps and just had some issues recently. First off I just wanted to say that everything...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.