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

How to get data out of geocoding #10

Closed
mediagirl opened this issue Sep 24, 2014 · 13 comments
Closed

How to get data out of geocoding #10

mediagirl opened this issue Sep 24, 2014 · 13 comments

Comments

@mediagirl
Copy link

I'm struggling to understand this part...

I have the address for each of my store locations geocoded. How do I get pieces of data out in the template?

For example, if I want to center my map (https://github.com/objectivehtml/Google-Maps-for-Craft/wiki/Set-Map-Center) and use the lat & long stored in the geocoding... how would I do that?

The code example on that page is:
{{ craft.googleMaps.center('map', 40, -86) }}

How do I get 40 and -86 to fill with the lat/long from the geocoding?

@keithmancuso
Copy link

the trick is to realize that each map field can have a number of different markers in it.. assuming you just have one marker in the map field it would be something like this

{% set options = { id: 'map', width: '400px', height: '300px' } %}
{{ craft.googleMaps.map(options) }}

{% for marker in entry.yourCustomMapField.getMarkers() %}
{% set lat = marker.lat %}
{% set lng = marker.lng %}
{% endfor %}

{{ craft.googleMaps.center('map',lat, lng) }}

Iif you just want to output the map for an entry is to use the field directly so just this, and it will automatically center on that marker

{% set options = { id: 'map', width: '400px', height: '300px' } %}
{{ craft.googleMaps.map(options) }}
{{ craft.googleMaps.data('map', entry.yourCustomMapField) }}

@mediagirl
Copy link
Author

Is this list of variables documented somewhere "marker.lat" and "marker.lng"?

@keithmancuso
Copy link

I dont think they are fully documented yet. I just went into the plugin files, in "models" theres GoogleMaps_MarkerModel and GoogleMaps_MapDataModel. in both of those look for the "defineAttributes()" function that will list the properties of the model

@keithmancuso
Copy link

ahh and he also has it written out in the wiki although there appears to be a typo as it says latitude vs lat
https://github.com/objectivehtml/Google-Maps-for-Craft/wiki/Template-Reference#marker

@mediagirl
Copy link
Author

Got it. I'll look there. Thank you! Was pulling my hair out yesterday trying to accomplish what I needed to accomplish for this project. Still having weird issues.

@mediagirl
Copy link
Author

When I try this code on a location's detail page with my custom field name added, I get no output.

{% for marker in entry.map.getMarkers() %}
    {% set lat = marker.lat %}
    {% set lng = marker.lng %}
{% endfor %}

{{ lat }} {{ lng }}

Do you?

@keithmancuso
Copy link

yup… is map your map fields handle?

Have you turned on geocoding ?

Geocoding has to be enabled in the plugin settings and you note which fields make up the address to geocode or on the field settings if you want it to geocode it as you edit the entry.

@mediagirl
Copy link
Author

Yes, "map" is my field's handle.

GeoCoding is on in plugin settings and I'm running the latest version #0.7.1

The fields that make up the address are set at the plugin level and the field level and the entry's marker is correctly set and saved in the DB.

I can get the map to show up on the detail page using this code but I want to control the zoom level and cannot do that using map() or data():

                {{ craft.googleMaps.map({
                    id: 'locationmap',
                    width: '385px',
                    height: '300px',
                    options: {
                        scrollwheel: false
                    }
                }) }}
                {{ craft.googleMaps.data('locationmap', entry.map) }}

@keithmancuso
Copy link

just use the zoom settings

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

so add this

{{ craft.googleMaps.zoom('locationmap', 12) }}

@mediagirl
Copy link
Author

I have tried that code and every other possible way to control the zoom. That code doesn't override the maps default zoom at all for me.

What works is setting "fitBounds: false" on map()... but then I need to get the marker's lat and long in order to center and zoom the map out... hence my question about how you get data out of the "geocoding'.

Will keep trying.

@mediagirl
Copy link
Author

NOTE: I can get lat & lng printing out in the template if I move the variables inside the for loop:

{% for marker in entry.map.getMarkers() %}
    {% set lat = marker.lat %}
    {% set lng = marker.lng %}
    {{ lat }} {{ lng }}
{% endfor %}

@mediagirl
Copy link
Author

ALAS... This finally worked!

{% for marker in entry.map.getMarkers() %}
    {{ craft.googleMaps.marker('locationmap', {
        lat: marker.lat,
        lng: marker.lng,
        fitBounds: false
    }) }}
    {{ craft.googleMaps.zoom('locationmap', 13) }}
    {{ craft.googleMaps.center('locationmap', marker.lat, marker.lng) }}
{% endfor %}

Keith, a sincere THANK YOU!

@objectivehtml
Copy link
Owner

Yup, thanks the way to do it. Thanks for the help Keith. Out of town on vacation this week with limited access to Internet.

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

3 participants