2

I have the coordinates (Long & Lat) that I'm having PHP insert all of them but I don't know how to change the javascript to correct the over query limit error.

here is the code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script type="text/javascript"  src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript"  src="markerclusterer.js"></script>

<script type="text/javascript"> 
        var map;

        //marker clusterer
        var mc;
        var mcOptions = {gridSize: 20, maxZoom: 17};

        //global infowindow
        var infowindow = new google.maps.InfoWindow();

        //geocoder
        var geocoder = new google.maps.Geocoder(); 

        var address = new Array(<?php echo(substr($point_str, 0, -2));?>);

        var content = new Array(<?php echo(substr($marker_str, 0, -2));?>);

        //min and max limits for multiplier, for random numbers
        //keep the range pretty small, so markers are kept close by
        var min = .999999;
        var max = 1.000001;

        function createMarker(latlng,text) {

            ///get array of markers currently in cluster
            var allMarkers = mc.getMarkers();

            //final position for marker, could be updated if another marker already exists in same position
            var finalLatLng = latlng;

            //check to see if any of the existing markers match the latlng of the new marker
            if (allMarkers.length != 0) {

                for (i=0; i < allMarkers.length; i++) {

                    var existingMarker = allMarkers[i];
                    var pos = existingMarker.getPosition();

                    //if a marker already exists in the same position as this marker
                    if (latlng.equals(pos)) {

                        //update the position of the coincident marker by applying a small multipler to its coordinates
                        var newLat = latlng.lat() * (Math.random() * (max - min) + min);
                        var newLng = latlng.lng() * (Math.random() * (max - min) + min);

                        finalLatLng = new google.maps.LatLng(newLat,newLng);

                    }                   
                }
            }

            var marker = new google.maps.Marker({
                position: finalLatLng
            });     

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.close();
                infowindow.setContent(text);
                infowindow.open(map,marker);
            });

            return marker;
        }

function geocodeAddress(address,i) {

geocoder.geocode( {'address': address}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) 

{
var marker = createMarker(results[0].geometry.location,content[i]); mc.addMarker(marker);
}

else

{
alert("Geocode was not successful for the following reason: " + status);
}
});
}

function initialize(){
var options = { 
zoom: 4, 
center: new google.maps.LatLng(39.8282,-98.5795), 
mapTypeId: google.maps.MapTypeId.ROADMAP 
};
map = new google.maps.Map(document.getElementById('map'), options); 

//marker cluster
mc = new MarkerClusterer(map, [], mcOptions);

for (i=0; i<address.length; i++) { 
geocodeAddress(address[i],i);
}
}
</script> 
<style>
html, body, #map {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body onload='initialize()'>
<div id="map"></div>
</body>
</html>

This is the HTML OUTPUT:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script type="text/javascript"  src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript"  src="markerclusterer.js"></script>

<script type="text/javascript"> 
        var map;

        //marker clusterer
        var mc;
        var mcOptions = {gridSize: 20, maxZoom: 17};

        //global infowindow
        var infowindow = new google.maps.InfoWindow();

        //geocoder
        var geocoder = new google.maps.Geocoder(); 

        var address = new Array("42.0619,-72.2136",
"28.06,-81.9561",
"30.3375,-81.7686",
"30.3375,-81.7686",
"41.7684,-88.1366",
"28.06,-81.9561",
"40.086,-82.486",
"40.6278,-79.0896",
"39.0197,-84.5914",
"41.8119,-87.6873",
"28.06,-81.9561",
"42.8698,-85.9697",
"42.8358,-85.6644",
"41.0309,-92.4098",
"39.1024,-94.5986",
"33.6115,-84.3745",
"34.84,-115.967",
"33.0807,-81.9868",
"41.7369,-73.7411",
"41.9214,-87.8924");

        var content = new Array("UnitNo1",
"UnitNo2",
"UnitNo3",
"UnitNo4",
"UnitNo5",
"UnitNo6",
"UnitNo7",
"UnitNo8",
"UnitNo9",
"UnitNo10",
"UnitNo11",
"UnitNo12",
"UnitNo13",
"UnitNo14",
"UnitNo15",
"UnitNo16",
"UnitNo17",
"UnitNo18",
"UnitNo19",
"UnitNo20");

        //min and max limits for multiplier, for random numbers
        //keep the range pretty small, so markers are kept close by
        var min = .999999;
        var max = 1.000001;

        function createMarker(latlng,text) {

            ///get array of markers currently in cluster
            var allMarkers = mc.getMarkers();

            //final position for marker, could be updated if another marker already exists in same position
            var finalLatLng = latlng;

            //check to see if any of the existing markers match the latlng of the new marker
            if (allMarkers.length != 0) {

                for (i=0; i < allMarkers.length; i++) {

                    var existingMarker = allMarkers[i];
                    var pos = existingMarker.getPosition();

                    //if a marker already exists in the same position as this marker
                    if (latlng.equals(pos)) {

                        //update the position of the coincident marker by applying a small multipler to its coordinates
                        var newLat = latlng.lat() * (Math.random() * (max - min) + min);
                        var newLng = latlng.lng() * (Math.random() * (max - min) + min);

                        finalLatLng = new google.maps.LatLng(newLat,newLng);

                    }                   
                }
            }

            var marker = new google.maps.Marker({
                position: finalLatLng
            });     

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.close();
                infowindow.setContent(text);
                infowindow.open(map,marker);
            });

            return marker;
        }

function geocodeAddress(address,i) {

geocoder.geocode( {'address': address}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) 

{
var marker = createMarker(results[0].geometry.location,content[i]); mc.addMarker(marker);
}

else

{
alert("Geocode was not successful for the following reason: " + status);
}
});
}

function initialize(){
var options = { 
zoom: 4, 
center: new google.maps.LatLng(39.8282,-98.5795), 
mapTypeId: google.maps.MapTypeId.ROADMAP 
};
map = new google.maps.Map(document.getElementById('map'), options); 

//marker cluster
mc = new MarkerClusterer(map, [], mcOptions);

for (i=0; i<address.length; i++) { 
geocodeAddress(address[i],i);
}
}
</script> 
<style>
html, body, #map {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body onload='initialize()'>
<div id="map"></div>
</body>
</html>

Thank you!

msvuze
  • 1,367
  • 2
  • 11
  • 21
  • could it be that the answer is already here: http://stackoverflow.com/questions/3529746/over-query-limit-while-using-google-maps – hwsw Jul 16 '13 at 16:39
  • If you have the latitude and longitude for the points, don't use the geocoder. However, I don't see anywhere in your code that contains the coordinates. – geocodezip Jul 16 '13 at 16:39
  • @geocodezip they are being inserter via .PHP var address = new Array(); and markers here: var content = new Array(); – msvuze Jul 16 '13 at 16:40
  • @bnz This is too different then what I have, I tried to change some stuff around but I just don't understand everything to make it work correctly, I think the function geocodeaddress is where it asks google.maps for the geometry location and then makes a marker but how do I replace this with my long and lat and remove everything else since I have that info. ? Thanks for the help – msvuze Jul 16 '13 at 16:59
  • @msvuze - I could see that. What is the data? address implies an address string, not coordinates. Is that where the coordinates are being sent? If so, how? Please update your question with more details. – geocodezip Jul 16 '13 at 17:17
  • @geocodezip just updated it. Thanks for helping me out, I really appreciate it a lot! – msvuze Jul 16 '13 at 17:23

1 Answers1

3

If you have the latitude and longitude for the points, don't use the geocoder.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script type="text/javascript"  src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclustererplus/src/markerclusterer.js"></script>


<script type="text/javascript"> 
        var map;

        //marker clusterer
        var mc;
        var mcOptions = {gridSize: 20, maxZoom: 17};

        //global infowindow
        var infowindow = new google.maps.InfoWindow();

        //geocoder
        var geocoder = new google.maps.Geocoder(); 

        var address = new Array("42.0619,-72.2136",
"28.06,-81.9561",
"30.3375,-81.7686",
"30.3375,-81.7686",
"41.7684,-88.1366",
"28.06,-81.9561",
"40.086,-82.486",
"40.6278,-79.0896",
"39.0197,-84.5914",
"41.8119,-87.6873",
"28.06,-81.9561",
"42.8698,-85.9697",
"42.8358,-85.6644",
"41.0309,-92.4098",
"39.1024,-94.5986",
"33.6115,-84.3745",
"34.84,-115.967",
"33.0807,-81.9868",
"41.7369,-73.7411",
"41.9214,-87.8924");

        var content = new Array("UnitNo1",
"UnitNo2",
"UnitNo3",
"UnitNo4",
"UnitNo5",
"UnitNo6",
"UnitNo7",
"UnitNo8",
"UnitNo9",
"UnitNo10",
"UnitNo11",
"UnitNo12",
"UnitNo13",
"UnitNo14",
"UnitNo15",
"UnitNo16",
"UnitNo17",
"UnitNo18",
"UnitNo19",
"UnitNo20");

        //min and max limits for multiplier, for random numbers
        //keep the range pretty small, so markers are kept close by
        var min = .999999;
        var max = 1.000001;

        function createMarker(latlng,text) {

            var marker = new google.maps.Marker({
                position: latlng,
            map: map
            });     

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.close();
                infowindow.setContent(text);
                infowindow.open(map,marker);
            });

            return marker;
        }

function initialize(){
  var options = { 
  zoom: 4, 
  center: new google.maps.LatLng(39.8282,-98.5795), 
  mapTypeId: google.maps.MapTypeId.ROADMAP 
  };
  map = new google.maps.Map(document.getElementById('map'), options); 

  var gmarkers = [];
  for (i=0; i<address.length; i++) {
    var ptStr = address[i];
    var coords = ptStr.split(",");
    var latlng = new google.maps.LatLng(parseFloat(coords[0]),parseFloat(coords[1]));               
    gmarkers.push(createMarker(latlng,content[i]));
  }

  //marker cluster
  mc = new MarkerClusterer(map, gmarkers, mcOptions);
}
</script> 
<style>
html, body, #map {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  }
</style>
</head>
<body onload='initialize()'>
<div id="map"></div>
</body>
</html>

working example

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • I found a small problem: if long and lat are the same the tooltip only shows for one. I asked a new question here http://stackoverflow.com/questions/17685144/google-maps-multiple-markers-for-same-location-showing-only-one-tooltip-message I'm also trying to see if I can change the long/lat a bit just to offset the marker(s) and make it clickable. – msvuze Jul 16 '13 at 20:04
  • 1
    The other option is to detect them either in your input routine on add or in the client when the marker is added and use a tabbed infoBubble to display the data for all the users at that location on a single marker. – geocodezip Jul 16 '13 at 20:34
  • I tried the OverlappingMarkerSpiderfier but I don't like that option because it moves the markers. I will try to see if I can make the tabbed infoBubble to display for the above code or just to combine the text from the multiple markers into the first one. Thanks again for the help geocodezip.com – msvuze Jul 16 '13 at 21:45