Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Grab Device Location and set Map to zoom to that Location

  • KhemrajC
  • KhemrajC's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 years 7 months ago #196230 by KhemrajC
Hi,

I am working on an urgent questionnaire to cover the on-going pandemic and I want users to provide their location from a Map-based Question.

I wish the map to automatically zoom to his area based on the stored device location and allow the user to fine tune his location and the updated coordinates have to be saved.

In case, the user is responding from a PC, the default IP could serve as a first guess for the location. However, the IP related Address is largely inaccurate for my little country.

I do also have a list of all localities and sub-localities.

Since the questionnaire is being generously hosted, I do not have access to the global settings.

Please guide me through for a smart solution.

I am completely new to Limesurvey and I have little programming experience.

Thank you very much.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 7 months ago #196233 by Joffm
Hi,
please have a look here.
Question of type "short free text"
www.limesurvey.org/manual/Question_type_...t_free_text#Location

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • KhemrajC
  • KhemrajC's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 years 7 months ago #196234 by KhemrajC
Hi,

Thanks for the prompt response.

I have already reached this step, however I wish to be able to automatically grab the device location through Javascript and the map to be centered accordingly and still allowing the user for fine tuning and the final position to be saved.

I came across this solution by Tony Partner:

www.limesurvey.org/community/forums/can-...coordinates?start=30

I do have the position of the device location, however the map is not reacting to zoom and center to that location. I also need the final position after manual fine tuning to be saved.

Thank you for guiding me through.

Best regards
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 7 months ago #196236 by Joffm
As you might have seen, tpartners script works with Google Maps.
Since some months you need a Google Maps API Key.
This you have to set in the Global Settings; but you said that you don't have access.

But with an equation you can use it with OpenStreetMap.

Example will follow in a few hours.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 7 months ago #196257 by Joffm
Example with OpenStreetMaps.
Because I am reqally not familiar with JS, I used tpartner's script, hided the question and entered the coordinates with EM into a following question that shows the OpenStreetMap.

I am sure this can be done more efficient, but it works.

Joffm

File Attachment:

File Name: Geo_locati...1276.lss
File Size:19 KB

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 7 months ago #196288 by Joffm
A slightly better - and shorter example.


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • KhemrajC
  • KhemrajC's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 years 7 months ago #196298 by KhemrajC
Hi,

Thanks for putting that effort for me. I am learning LS on fast-track to design a questionnaire for the on-going pandemic to freely assist our community and Govt.

I just imported the Survey and I changed the map from google to openstreetmap and after execution, I filled in the Survey Form from my laptop and smartphone.

Voila, its working on both.

The location data is being saved and even the hidden fields are saved (that is very smart).

The only issue is with the zoom level, I changed it from 4 to 11, however it is persisting on 4.

With an appreciable zoom level, it would better assist the user to choose a rough location.

Any suggestion to fix it?

Can the "Restrict Search Place to Map Extent" be ticked by default ?

Anyway this search field is not that useful for my little country as openstreetmap is not enough covered and there are minimum landmarks and street names, especially for the massively inhabited rural areas.

A sort of autocomplete from a file or array can be more useful. From Geonames, they do provide a list with localities with corresponding longitude and latitude. Since I do not have access to the config file, I cannot directly add options to use Geonames/Mapquest API.

Any suggestion or example ?

Thanks and regards
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 7 months ago - 4 years 7 months ago #196314 by Joffm
In my second example there is set the zoom level to 11, and it keeps this level in my installation 3.22.2.
By the way, which version are you using? You still did not mention that.


If I change the level to 4 , I see this.



Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 years 7 months ago by Joffm.
The topic has been locked.
  • KhemrajC
  • KhemrajC's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 years 7 months ago #196352 by KhemrajC
Hi,

Same version 3.22.

In your LSS, the zoom level was at 4 and when I changed it to 11, I kept having the same zoom level.

Same result after changing zoom level to 16.

Thanks and regards
The topic has been locked.
  • KhemrajC
  • KhemrajC's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 years 7 months ago #196357 by KhemrajC
Hi,

I just figured out the mistake that I was doing for the zoom level.

In fact, your solution has a different structural approach than TPartners though the in-line Javascript is almost identical.

I was changing the zoom level for question q1 which contains the GPS script and nothing was happening.

Now I found that q1a is just another short text question with openstreetmap selected and you have a tweak code at the default location.

When changing the zoom level here, it reflects in the execution.

{str_replace(";"," ",q1)}

What is the logic behind the above Expression Script ?

My country is not well covered in openstreetmap with only few street names and sub-localities.

Can I have the "Restrict Search Place for Map Extent" to be selected by default ?

Or how I get rid of that search text input since it does not proper location suggestion ?

Thank you very much.

Best regards
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 7 months ago #196358 by Joffm
Hi,
I just realized that I did not attach my second example.

Try this. It is shorter and doesn't rely on something else than the navigator object.

File Attachment:

File Name: limesurvey...1276.lss
File Size:18 KB

{str_replace(";"," ",q1)}

This? It's a simple string replace. The colon is changed to "blank".
www.limesurvey.org/manual/ExpressionScri...mplemented_functions

As you might have seen in Tony's script the longitude and latitude are separated by colon.
But OpenStreetMap expects that they are separated by "blank".

In the attached example it isn't necessary anymore. The javascript part return the coords separated by "blank".

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • KhemrajC
  • KhemrajC's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 years 7 months ago #196359 by KhemrajC
Hi,


Its working great. Thank you.


Prior to replicate the code in my master questionnaire, I imported all 3 questions into a blank project as a test case but nothing happened after execution.


Attached is the corresponding LSS.


Please let me know of the reason.


Thanks and regards
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose