You are viewing limited content. For full access, please sign in.

Question

Question

Map image from Geolocation not showing

asked on July 8, 2018

Hi All,

Having an issue with 10.3.1 forms,

We are trying to have the image captured, displayed and saved into a form for the geolocation.

The issue is we can't get the image to show in the form.

This exact setup is working in another environment using 10.2 so not sure what is causing the problem.

How we have it setup is:-

3 fields

A single line field to hold the Geolocation value #q4

A HTML field "P Tab" for the written values #q2

A HTML field "Canvas" for the image (HTML:-  <canvas id="myCanvas"></canvas>)

Here is the JavaScript below we use and I have attached images showing the issue

 

$.getScript('https://maps.googleapis.com/maps/api/js?key=KEYNUMBER&libraries=places', function() {
    //$('#q4').hide();
  getLocation();
   function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      document.getElementById("q2").innerHTML = "Geolocation is not supported by this browser.";
    }
  }
   function showPosition(position) {
    $('#q4 input').val(position.coords.latitude + "|" + position.coords.longitude);
    drawMap($('#q4 input').val());
  }
   if ($('#q4').prop('tagName') != 'INPUT') {
    var divlatlong = document.getElementById("q4").innerHTML;
    drawMap(divlatlong);
  }
   function drawMap(n) {
    var position =  n.split('|');
    var latitude = position[0];
    var longitude = position[1];
    document.getElementById("q2").innerHTML = "Location found!<br />Latitude: " + latitude + "<br />Longitude: " + longitude;
    var google_tile = "http://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=16&size=400x150&markers=color:red|" + latitude + "," + longitude;
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var img = new Image();
    img.src = google_tile;
    img.onload = function(){
      context.drawImage(img, 0, 0);
    }
  }
   });
 
Regards
Shane
Canvas.JPG
Java.JPG
Image form.JPG
Canvas.JPG (31.07 KB)
Java.JPG (107.09 KB)
Image form.JPG (32.96 KB)
0 0

Answer

SELECTED ANSWER
replied on July 10, 2018

Hi Shane,

 

I noticed you are trying for a static map? Do you need a marker on the map? if not, and just after a general location try this..

I'm sure you could alter the url address to add a marker if required, but this is working for me.

$(document).ready(function(){
 
  getLocation();
   function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      document.getElementById("q2").innerHTML = "Geolocation is not supported by this browser.";
    }
  }
   function showPosition(position) {
// Input field to show latitude and longitude
    $('#q6 input').val(position.coords.latitude + "|" + position.coords.longitude);
  drawMap();
   }
  
  function drawMap(){
  var langLong = $('#q6 input').val();
  var position = langLong.split('|');
  var lang = position[0];
  var long = position[1];

 //Custom html field with id q1
  document.getElementById('q1').innerHTML = "<img src='https://maps.googleapis.com/maps/api/staticmap?center="+lang+","+long+"&zoom=16&size=400x400&"
            +"key=[INSERT YOUR API KEY HERE]' height='400' width='400'>";
  }
});

 

0 0

Replies

replied on July 10, 2018

Thanks Aaron,

Not having the marker is not a big issue as they have the LAT/LONG coordinates if required.

Also by changing the zoom amount to 17.25

Something I have just found though, Google API require a credit card to sign up now even for the free account!!!

They are dreaming.

I have been looking around and used MapBox instead https://www.mapbox.com

All I had to do was replace the img address and use my MapBox API key

I also changed the pitch with Mapbox so the map is not directly over the top which makes it look a little nicer, it is the ,60/ in the site address line below

 

$(document).ready(function(){
 
  getLocation();
   function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      document.getElementById("q2").innerHTML = "Geolocation is not supported by this browser.";
    }
  }
   function showPosition(position) {
// Input field to show latitude and longitude
    $('#q4 input').val(position.coords.latitude + "|" + position.coords.longitude);
  drawMap();
   }
 
  function drawMap(){
  var langLong = $('#q4 input').val();
  var position = langLong.split('|');
  var lang = position[0];
  var long = position[1];
 //Custom html field with id q5
  document.getElementById('q5').innerHTML = "<img src='https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/"+long+","+lang+",17.25,0,60/600x600?access_token=MY MAPBOX API KEY'>";
  }
});
 
Regards
Shane
2 0
replied on July 10, 2018

Hey Shane,

Did you ever find the resolution to this post?

 

0 0
replied on July 10, 2018 Show version history

Hi Chase,

 

Looks like I have it working, have details in this now

 

Regards

Shane

0 0
replied on July 10, 2018

Something to note as well though about the new Google API Credit Card addition is it's free until the monthly limit is hit, then overage charges apply to the Credit Card assigned.  If it's a smaller implementation that will never reach 28,000 map requests a month you shouldn't be too deterred from using Google.

But I do agree it is ridiculous.

 

API Changes Introduced By Google

 

Recently, Google Maps have announced certain API changes that will affect everyone using Google Maps. Essentially, Google has implemented new pay-as-you-go pricing plans for their API. On this plan mention, users will get up to 28 000 map requests per month and 40 000 direction calls per month, free of charge.

0 0
replied on July 10, 2018

Yes I did see that, I don't agree with them asking for credit cards for something that I will never go over.

Asking customers for Credit cards for the API in their small businesses that we deal with is going to be pushed back by a lot.

We will still offer customers google maps and they can sign up for it

Reading through Google cloud services it does seem that if there are any errors in billing they may just credit your account not put it back on the credit card not what I'm looking for on a free account.

Especially for testing just one less place my credit card details are :-)

 

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.