Page 1 of 1
[SOLVED] CGGoogleMaps2 gets My Location wrong
Posted: Wed Oct 22, 2014 3:25 pm
by Guido
I'm wondering if I'm doing something wrong: I installed CGGoogleMaps2, created a map, created a marker and called the map on my site, including the 'directions' portion.
Now when I choose the marker as my destination, and use 'My Location' as starting point, the map will generate a route that starts from a place roughly 40 km from here. When the initially loads it asks me if I want to share my location, I clicked 'yes'.
Re: CGGoogleMaps2 gets My Location wrong
Posted: Wed Oct 22, 2014 8:07 pm
by paulbaker
Pretty sure that's got nothing to do with CGGoogleMaps2. It's more to do with Google Maps and it wanting location information from your browser. Are you on a desktop computer? The location info is just a guess based on your IP, as far as I am aware.
A fairer test would be bringing up the same page using a mobile phone browser with GPS ability - this should get the location exactly right.
Re: CGGoogleMaps2 gets My Location wrong
Posted: Wed Oct 22, 2014 8:24 pm
by calguy1000
When I use my desktop, Google maps gets my location as over 200km away. It's based entirely on geographic IP maps....
When I use a VPN it's much worse
Re: CGGoogleMaps2 gets My Location wrong
Posted: Wed Oct 22, 2014 8:25 pm
by Guido
Seems fair. I just did a test: I went to maps.google.nl and entered the same destination. When I picked 'plan route' it didn't let me choose 'current location', maybe Google maps has problems determining my exact location on a desktop.
Re: CGGoogleMaps2 gets My Location wrong
Posted: Wed Oct 22, 2014 8:40 pm
by calguy1000
GPS's are usually built into phones and tablets... not desktop machines or laptops.
Therefore the only thing that google can do when determining a location for a desktop or a laptop is a best guess based on either an increasingly inaccurate geographical->ip map, or maybe some kind of IP address -> domain -> city -> lat/long lookup.
Re: CGGoogleMaps2 gets My Location wrong
Posted: Thu Oct 23, 2014 9:21 am
by Guido
OK, thanks for your answers. For desktops I'm going to get rid of the 'from' dropdown and turn it into a manual input field.
Re: [SOLVED] CGGoogleMaps2 gets My Location wrong
Posted: Thu Oct 23, 2014 10:44 am
by Guido
I was wondering if someone could help me on my way a bit:
I want to modify the directions template so that it accepts a text input in stead of an options selector to enter the 'from' part. So first I have to change:
Code: Select all
{$mod->Lang('to')}: <select id="to_location_{$mapinstance}"/></select>
To and <input> field. But the getDirections is triggered from the changing of one of the select fields here:
Code: Select all
$('#cggm_map_{$mapinstance}').cggm2('getDirections',list,function(res) {
if( typeof res == undefined || res == null ) {
$('#directions_error_{$mapinstance}').html('{$mod->Lang('error_directionsfailed')}').show();
}
So I have to change this to a 'click' event and add something to actually click. I've tried this, but it doesnt work. Can someone give me some pointers on this?
Also, i'd like to add the step-by-step directions, I've searched for google's v3 API on this but I'm not getting the fill gist of it yet.
Re: [SOLVED] CGGoogleMaps2 gets My Location wrong
Posted: Thu Oct 23, 2014 12:23 pm
by Guido
I am now this far:
Code: Select all
{* directions form template *}
{* this template is used when directions are enabled. The default plugin calls the following 2 plugin methods:
'getMarkerList' retrieves a list of the markers currently availablein the map.
'getDirections' to get direction output
*}
<__script__ type="text/javascript">
if( typeof(jQuery) == 'undefined' ) {
var div = document.getElementById('cggm_map_{$mapinstance}').
div.innerHTML = '<h3 style="color: red;">jQuery and jQuery UI Are Required</h3>';
throw new Error('jQuery and jQuery UI Are Required');
}
$(document).ready(function(){
$('#cggm_map_{$mapinstance}').on('cggm2update',function(){
var locations = $('#cggm_map_{$mapinstance}').cggm2('getMarkerList');
//$('#from_location_{$mapinstance}').find('option').remove();
$('#to_location_{$mapinstance}').find('option').remove();
for( var i = 0; i < locations.length; i++ ) {
var loc = locations[i];
//$('#from_location_{$mapinstance}').append($('<option>', { value: loc.value, text: loc.text }));
$('#to_location_{$mapinstance}').append($('<option>', { value: loc.value, text: loc.text }));
}
$('#test').click(function(){
var list = [];
list.push($('#from_location_{$mapinstance}').val());
list.push($('#to_location_{$mapinstance}').val());
$('#cggm_map_{$mapinstance}').cggm2('getDirections',list,function(res) {
if( typeof res == undefined || res == null ) {
$('#directions_error_{$mapinstance}').html('{$mod->Lang('error_directionsfailed')}').show();
}
});
});
}); // update event
});
</__script>
<fieldset id="directions_form_{$mapinstance}">
<legend>{$mod->Lang('prompt_directions')}</legend>
{$mod->Lang('from')}: <input type="text" id="from_location_{$mapinstance}"></input>
{$mod->Lang('to')}: <select id="to_location_{$mapinstance}"/></select>
<div id='test' style='width:100px;height:100px;background:red;'></div>
<div id="directions_error_{$mapinstance}" class="alert alert-error" style="display: none;"></div>
</fieldset>
So now, when you click the red div named 'test', it will fire the getDirections function. But nothing happens because it isn't taking the input from the <input> I created. Basically I want to add the input to the 'list' array that the getDirections function uses. I can't find any pointers on how to use the getDirections either, does it use the two values of the list array in the order of 'fromaddress, toaddress'? Anyone have any tips?
============== EDIT ============
I found out what the problem is in the above code. The input from either the <input> field or the <select> option doesn't send the input directly to the google maps API, it first looks up a location in the CGGoogleMaps2 map, one that's predetemined by a user. So when I create a location for a map and enter that exact string in the input text field, it'll work. Now I need to find a workaround for this.