Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map Appears to be Limiting the Number of Markers Shown #32

Open
DigitalHodgePodge opened this issue Mar 19, 2015 · 29 comments
Open

Map Appears to be Limiting the Number of Markers Shown #32

DigitalHodgePodge opened this issue Mar 19, 2015 · 29 comments

Comments

@DigitalHodgePodge
Copy link

I'm running into an issue when using {{ craft.googleMaps.marker('mapID', markerInfo) }} to add markers to the map. It appears I'm being limited to 10 markers; however, the loop that this is found in is returning 150 entries.

Is there a way that I can display more markers on the map? I was looking for a parameter that I might be able to set in the map's options, but I wasn't able to find anything.

@Floriswijgergangs
Copy link

I've got the same problem, any solution available?

@DigitalHodgePodge
Copy link
Author

I'm not at my computer at the moment unfortunately, but I was able to find a workaround to get the markers to populate. I will try to find the code and post it later, but basically, I added the markers (their address, location, details, etc.) to an array and then iterated through that using the GoogleMaps function that you'll find at the bottom of your page if you inspect the source.

Hopefully, that makes sense. Again, I'll try to get something up later that's a bit more detailed.

@Floriswijgergangs
Copy link

Thanks for your reaction, looking forward to see your code. Thanks!

@DigitalHodgePodge
Copy link
Author

Hopefully, this makes some sense. Tried to take out some of the stuff and reassemble.

<script type="text/javascript">
    //CREATE THE ARRAY
    var markerArray = [];

    //LOOP OVER THE ENTRIES WHICH IN MY CASE ARE PROFILES
    {% for profile in profiles %}

        //SET THE MARKERS
        {% if profile.residentLocation.markers is not empty %}

                //SET "geoaddress" VARIABLE EQUAL TO <latitude>,<longitude> OF MARKER
                {% set geoaddress = profile.residentLocation.markers[0].lat~","~profile.residentLocation.markers[0].lng %}

                //I WANTED THE PHONE NUMBER IN THE CONTENT OF THE MARKER
                {% if profile.phone is not empty %}
                    {% set slicedphone = profile.phone | slice(0, 3) ~'.'~profile.phone | slice(3, 3) ~'.'~profile.phone | slice(6, 4) %}
                {% else %}
                    {% set slicedphone = profile.phone %}
                {% endif %}


                //HERE I'M CREATING AN OBJECT WITH ALL OF THE INFO I WANT IN THE MARKER
                currentArray = {
                    title: "{{profile.title}}",
                    lat: "{{profile.residentLocation.markers[0].lat}}",
                    lng: "{{profile.residentLocation.markers[0].lng}}",
                    content: "{{profile.title}}<br>{{profile.residentLocation.markers[0].address}}<br>{{slicedphone}}<br><a href=\"/{{profile.uri}}\">View Profile</a><br><a href=\"http://maps.google.com/?q='{{profile.residentLocation.markers[0].address}}\" target=\"_blank\">Get Directions</a>",
                    customContent: true,
                    address: "{{geoaddress}}",
                    isNew: false,
                    deleted: false
                };

                //AS I FINISH THE LOOP, I ADD THE currentArray OBJECT TO THE markerArray ARRAY
                markerArray.push(currentArray);


        {% endif %}

    {% endfor %}

    //FINALLY, I USE THE COMPLETED markerArray TO POPULATE THE MARKERS INTO THE MAP WITH ID "mainMap" (YOU MAY NOT NEED THE DELAY FROM setTimeout)
    $(function(){
        $(window).load(function() {
            setTimeout(function mapMarkerLoad() {
                new GoogleMaps.MapData(mainMap, {
                    "markers": markerArray,
                    "polylines": [],
                    "routes": [],
                    "circles": [],
                    "groundOverlays": []
                }, []);
            }, 100);
        })
    });

</script>

EDIT: Also, I probably should have clarified that this script is in a Craft Twig template file. (Hence all the braces)

@objectivehtml
Copy link
Owner

I have no idea what would cause the script to limit you to only 10 markers on the map. I will do some tests locally, but I really don't know why this would be occurring. There shouldn't be anything in the code to limit you to only 10 markers.

So do you have 150 entries each with one marker? Or does each entry have multiple markers?

@Floriswijgergangs
Copy link

every entry has one address. its an website for real estate company.
So every marker is an house for sale.

@objectivehtml
Copy link
Owner

When you view the source of the page, do you see more than 10 instances of the "new GoogleMaps.Marker(...)"?

@Floriswijgergangs
Copy link

var map = new GoogleMaps.Map(document.getElementById("oh-map-map"), {"maxZoom":14,"api":true});

new GoogleMaps.Marker(map,{"address":"Patrijsstraat87Neede","content":"Patrijsstraat 87 Neede"});

new GoogleMaps.Marker(map,{"address":"Pastoor Scheepersstraat33Vragender","content":"Pastoor Scheepersstraat 33 Vragender"});

new GoogleMaps.Marker(map,{"address":"Kruiskamplaan138Eibergen","content":"Kruiskamplaan 138 Eibergen"});

new GoogleMaps.Marker(map,{"address":"Hoogkamp85Eibergen","content":"Hoogkamp 85 Eibergen"});

new GoogleMaps.Marker(map,{"address":"St Isidorushoeveweg23Rietmolen","content":"St Isidorushoeveweg 23 Rietmolen"});

new GoogleMaps.Marker(map,{"address":"Meidoornstraat31Eibergen","content":"Meidoornstraat 31

and many more....

@objectivehtml
Copy link
Owner

Right, so I see the issue. The issue is Google has a limit to the number of geocoder requests you can perform consecutively, so this is the issue. For some reason, the latitude and longitude isn't being added to the marker objects, so the script has to geocode the address for each marker. I am doing some testing now, I will see if I get this issue locally.

@Floriswijgergangs
Copy link

Thanks, keep me updated:)

@objectivehtml
Copy link
Owner

Can you post your template code?

@Floriswijgergangs
Copy link

{% set options = {
    id: 'map', 
    width: '100%', 
    height: '900px',
    maxZoom: 14,
} %}
{{ craft.googleMaps.map(options) }}  
{% for entry in craft.entries.section('koopWoningen') %}
{% set city = entry.plaatsnaam %}
{% set street = entry.straat %}
{% set housenr = entry.huisnummer %}
{% set combinedata = street ~" "~ housenr ~" "~ city %}
{% set marker = {
    address: street ~ housenr ~ city ,
    content: combinedata,
} %}
 {{ craft.googleMaps.marker('map', marker) }}
{% endfor %}

@objectivehtml
Copy link
Owner

Ok, so you aren't using the same code as the person who opened this thread. Are you using the field type to store your locations in the db? The field type geocodes each location and stores that in the db so each marker has a valid lat/lng so you don't have to perform additional geocoding requests to plot your data.

@Floriswijgergangs
Copy link

I use an XML to import (Feedme plugin) the houses into craft and I’m not able to put the addresses into the field group created by the plugin. So its not possible to geocode them on the server side with the plugin fields.

Op 7 apr. 2015, om 16:57 heeft Justin Kimbrell <[email protected] mailto:[email protected]> het volgende geschreven:

Ok, so you aren't using the same code as the person who opened this thread. Are you using the field type to store your locations in the db? The field type geocodes each location and stores that in the db so each marker has a valid lat/lng so you don't have to perform additional geocoding requests to plot your data.


Reply to this email directly or view it on GitHub #32 (comment).

@DigitalHodgePodge
Copy link
Author

Right, so I see the issue. The issue is Google has a limit to the number of geocoder requests you can perform consecutively, so this is the issue. For some reason, the latitude and longitude isn't being added to the marker objects, so the script has to geocode the address for each marker. I am doing some testing now, I will see if I get this issue locally.

Interesting. I also had multiple instances of "new GoogleMaps.Marker(...)" on my page as well. It made sense that Google might have had a limit, but I hadn't found anything when I had done my search.

I'm curious to see what you find while testing locally—though, as I said, it's currently functioning with the workaround on my site.

@objectivehtml
Copy link
Owner

@Floriswijgergangs Well this is a pretty hard requirement. Google Maps for Craft has the ability to geocode address fields to a location when an entry is saved. Go to the plugin settings and setup the geocoder for your section. If the import plugin triggers core Craft methods to save the entry, the events should be triggered properly so your entries can be geocoded automatically. Without lat/lng coordinates for each entry, you will be constantly running into this issue. Is the geocoder method doesn't work, you'll have to figure something else out. This isn't a limitation of Google Maps for Craft, but a limitation of Google Maps API itself.

@Floriswijgergangs
Copy link

Thanks for the feedback I’m trying to use the server side geocode.

Ive include an googlemap field in the section ’koopwoningen’.
With the automatic geocode configured.
When I open an entry the google map field preview is displaying the correct information.
However when I save the entry. I get an php error.:

PHP warning

Invalid argument supplied for foreach()

/Users/Floris/craft-1/worm/craft/plugins/googlemaps/GoogleMapsPlugin.php(113)

101 // If no address was entered, return script with no changes to db.
102 if(!count($address))
103 {
104 return;
105 }
106
107 $response = craft()->googleMaps_geocoder->geocode($address);
108
109 if($response->status == 'OK')
110 {
111 $geocodeResponse = $response->results[0];
112
113 foreach($settings->mapFields as $mapFieldId)
114 {
115 $mapField = craft()->fields->getFieldById($mapFieldId);
116 $mapData = json_decode($content->{$mapField->handle});
117
118 $mapDataModel = new GoogleMaps_MapDataModel((array) $mapData);
119
120 // Update marker if exists, otherwise add a new marker
121 if($marker = $mapDataModel->getMarker(0))
122 {
123 $marker->lat = $geocodeResponse->geometry->location->lat;
124 $marker->lng = $geocodeResponse->geometry->location->lng;
125 $marker->address = $geocodeResponse->formatted_address;
Stack Trace
#0
– /Users/Floris/craft-1/worm/craft/plugins/googlemaps/GoogleMapsPlugin.php(113): CApplication->handleError(2, "Invalid argument supplied for foreach()", "/Users/Floris/craft-1/worm/craft/plugins/googlemaps/GoogleMapsPl...", 113, ...)
108
109 if($response->status == 'OK')
110 {
111 $geocodeResponse = $response->results[0];
112
113 foreach($settings->mapFields as $mapFieldId)
114 {
115 $mapField = craft()->fields->getFieldById($mapFieldId);
116 $mapData = json_decode($content->{$mapField->handle});
117
118 $mapDataModel = new GoogleMaps_MapDataModel((array) $mapData);
#1
unknown(0): Craft\GoogleMapsPlugin->Craft{closure}(Craft\Event)
#2

Op 7 apr. 2015, om 17:23 heeft Justin Kimbrell <[email protected] mailto:[email protected]> het volgende geschreven:

@Floriswijgergangs https://github.com/Floriswijgergangs Well this is a pretty hard requirement. Google Maps for Craft has the ability to geocode address fields to a location when an entry is saved. Go to the plugin settings and setup the geocoder for your section. If the import plugin triggers core Craft methods to save the entry, the events should be triggered properly so your entries can be geocoded automatically. Without lat/lng coordinates for each entry, you will be constantly running into this issue. Is the geocoder method doesn't work, you'll have to figure something else out. This isn't a limitation of Google Maps for Craft, but a limitation of Google Maps API itself.


Reply to this email directly or view it on GitHub #32 (comment).

@objectivehtml
Copy link
Owner

Can you take a screenshot of how you have your settings configured? I think that error is being thrown because you haven't defined any Google Maps fields.

@Floriswijgergangs
Copy link

In attachment print screens.

Op 7 apr. 2015, om 17:53 heeft Justin Kimbrell <[email protected] mailto:[email protected]> het volgende geschreven:

Can you take a screenshot of how you have your settings configured? I think that error is being thrown because you haven't defined any Google Maps fields.


Reply to this email directly or view it on GitHub #32 (comment).

@objectivehtml
Copy link
Owner

@DigitalHodgePodge I am testing now for multiple markers (more than 10) and it's working for me. Here is me code I am using to test currently. I am just using the data method which will plot all the data from your field type on a map.

{% set query = craft.request.getParam('q') %}

{% set params = {
    address: query,
    distance: 200,
    unit: 'kilometers'
} %}

{% set options = {
    id: 'map', 
    width: '400px', 
    height: '300px',
    clustering: true
} %}

{{ craft.googleMaps.map(options) }}

<ul class="search-results">
{% set entries = craft.entries.section('news').map(params).order('distance asc') %}
{% for entry in entries %}
    <li>
        <h3>{{ entry.title }}</h3>

        {{ craft.googleMaps.data('map', entry.map) }}

        {% for marker in entry.map.markers() %}
            <p>Distance - {{ marker.distance }}</p>
        {% endfor %}
    </li>
{% endfor %}
</ul>

@objectivehtml
Copy link
Owner

@Floriswijgergangs I am not seeing that screenshot. It doesn't seem to have uploaded correctly. Can you try again?

@Floriswijgergangs
Copy link

schermafbeelding 2015-04-07 om 17 54 35
schermafbeelding 2015-04-07 om 17 54 24

@objectivehtml
Copy link
Owner

I think there is a bug when you check the "all" option. Can you uncheck the top option and just select the "mapsaddress" field to see if that fixes that PHP error?

@Floriswijgergangs
Copy link

Yeah workin:).
How can I retrieve the lat en lng and translate it into one map with all markers.
{{ entry.mapsaddress.markers.lat }} does not work.

Key "lat" for array with keys "0" does not exist

Op 7 apr. 2015, om 18:00 heeft Justin Kimbrell <[email protected] mailto:[email protected]> het volgende geschreven:

I think there is a bug when you check the "all" option. Can you uncheck the top option and just select the "mapsaddress" field to see if that fixes that PHP error?


Reply to this email directly or view it on GitHub #32 (comment).

@objectivehtml
Copy link
Owner

Look at the code example I posted above in reference to the other person posting in the thread. You are getting that error because entry.mapsaddress.markers returns and array of objects. Your syntax is implying that it's returning an object.

@jonlongnecker
Copy link

I'm running into the same problem here even though I've entered all my locations manually and there are latitude/longitude values in the database. Looking at my source I am getting new instances of GoogleMaps.Marker for each location on the map.

Should I be passing lat/long values instead of address in the marker field?

{% set options = {
        id: 'map', 
    width: '100%', 
  height: '400px',
} %}

        {% set map = craft.googleMaps.map(options) %}

         {% for markerCustom in entry.blogLocations.getMarkers() %}
            {% set address = '<div class="marker-content"><h3>' ~ markerCustom.title ~ '</h3><p>' ~ markerCustom.content ~ '</p></div>' %}
            {% set marker = {
               address: markerCustom.address, 
               content: address,
            } %}
            {{ craft.googleMaps.marker('map', marker) }}
        {% endfor %}

        {{ map }}

@objectivehtml
Copy link
Owner

Use the latitude and longitude so the address doesn't have to be geocoded with every page load. Always use the lat/lng when available to display markers over using the address. The address is just convenient for very small and static datasets.

@jonlongnecker
Copy link

Got it. So is there a index of the tags available? I finally got it to work with lat: markerCustom.lat and lng: markerCustom.lng, but didn't see it anywhere in the wiki.

@objectivehtml
Copy link
Owner

objectivehtml commented Jun 8, 2016

Yeah, I think the only place I have it spelled out to use the lat/lng in this use case is here.

https://github.com/objectivehtml/Google-Maps-for-Craft/wiki/Template-Reference#marker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants