Im working on a script that will pull a variable from my MySQL db table, heres the example coding google gives me.
var beaches = [
['Bondi Beach', -33.890542, 151.274856, 4],
];
and now here is what im trying to achieve,
i have a table with the fields, ID , Beach , Long, Lat
how would i replace the beach variable to pull from my MySQL table instead of having to go in and manually add the beach to the variable. That way when users add a Beach Name with longitude and latitude to the DB through my form it automatically adds a marker on my google maps.
I am achieving the ComplexIcon Overlays with Google Maps API v3
https://developers.google.com/maps/documentation/javascript/overlays#ComplexIcons
Im guessing im going to be using some AJAX ? I have never used AJAX before so if this is the case i guess i better pull up my AJAX tuts :)
Javascript in a user's browser can't get at your database directly (which is good!) but it can ask your server for data, and AJAX may well be the way to go if you want dynamic updates. Google has an article: https://developers.google.com/maps/articles/phpsqlajax_v3
However it seems more likely that you will simply need to get the marker data when you send the page to the browser, and you can do that by constructing the page in PHP or another server-side language, using similar techniques to get data out of the database and use the values directly in the page code.
You may need to do both, to create the initial page the user gets and update it via AJAX so just the data changes and you don't have to refresh the whole page.
[Note: you don't have to use XML to transfer data asynchronously, you could use JSON to format it or any other format you can code for. But XML is easy and there are plenty of examples.]
Yes, you have to do it with ajax.
For example, I'll use jQuery
$.ajax({
url: formSubmitUrl,
data: 'id=1&lat=23.444...',
type: 'GET',
success: function (data)
{
// data = response from the script
// if every thing is ok, I return the string 'OK'
if(data == 'OK')
{
var pMarker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
icon: yourMarkerimage
});
}
}
});
And that's all from the client side.
Submit the form via AJAX and then add the marker to the map
Related
I am using google maps to provide directions to multiple locations within a website. Users are in Japan, but are non-Japanese, so results should be in English.
In certain examples, even when the name is in the query parameter, a link like this location, returns an alternate Japanese place name (主教座聖堂牧師館), instead of "St. Andrew's Tokyo."
This link is dynamically generated, so I can change the parameters if need be, but I can't figure out how to force results that look more like this, without hardcoding the entire link. Here is what builds the URL:
//handle directions links; send to Apple Maps (iOS), or Google Maps (everything else)
var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
$body.on('click', 'a.tsml-directions', function(e){
e.preventDefault();
var directions = (iOS ? 'maps://?' : 'https://maps.google.com/?') + $.param({
daddr: $(this).attr('data-latitude') + ',' + $(this).attr('data-longitude'),
saddr: 'Current Location',
q: $(this).attr('data-location')
});
window.open(directions);
});
I've had a look at your sample URL https://www.google.com/maps?daddr=35.6603676,139.7444553&saddr=Yotsuya,%20Shinjuku,%20Tokyo%20160-0004&q=St.%20Andrew%27s%20Tokyo.
I understand that your intention is getting a directions on Google Maps. In the aforementioned URL you specify parameters for origin saddr and destination daddr, the q parameter shouldn't affect directions in this case. So, the destination address is just coordinate 35.6603676,139.7444553. When I reverse geocode this coordinate I get the 'Japan, 〒105-0011 Tōkyō-to, Minato-ku, Shibakōen, 3 Chome−6−18 主教座聖堂牧師館' address as shown in Geocoder tool:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D35.660368%252C139.744455
The 主教座聖堂牧師館 corresponds to premise address component and I suspect it is not translated to English in Google database, because web service call with language set to English returns this component in original language as well
https://maps.googleapis.com/maps/api/geocode/json?latlng=35.6603676%2C139.7444553&language=en&key=YOUR_API_KEY
If your destination should be St. Andrew's, use it as a destination parameter.
And the most important part: Google has Google Maps URLs as an official, recommended and documented method to construct URLs. I would suggest using this API in order to create your directions URLs. These URLs are cross-platform, so there is no need to create different URLs for iOS, Android or web browser.
Your example will convert into
https://www.google.com/maps/dir/?api=1&origin=Yotsuya,%20Shinjuku,%20Tokyo%20160-0004&destination=St.%20Andrew%27s%20Tokyo&travelmode=driving
The result is shown in the screenshot
Your code might be something like
$body.on('click', 'a.tsml-directions', function(e){
e.preventDefault();
var directions = 'https://www.google.com/maps/dir/?' + $.param({
api: "1",
destination: $(this).attr('data-location'),
travelmode: "driving"
});
window.open(directions);
});
Note I don't specify origin parameter as it is optional and in this case it should be interpreted as my current location.
I hope this helps!
I tried to change some values in the data layer before it reach my Google analytics account.
I used a custom HTML tag to manipulate the data Layer. Say I wanted to multiply my room count variable by 3,
<script type="text/javascript">
(function() {
var var7 = ({{DL_roomcount}})
var7 = 3*var7
dataLayer.push({
'roomcount' : var7
});
})();
</script>
Apparently, even though it shows the new values in the data Layer in preview mode, the new values never reached the analytics account.
The trigger type I used was - custom event & event name was 'gtm.load'. In preview mode datalayer shows roomcount as 3 but in analytics account its still 1.
My GA-debug console also shows 1 instead of 3.
Any help regarding this would be highly appreciated.
Thank you
You should push an event into the dataLayer at the same time that you are pushing the new value for roomcount so that you can access that new value:
// your other code
dataLayer.push({
'event': 'update roomcount',
'roomcount': var7
})
You can now access the new roomcount value if you trigger your tag to fire on the update roomcount event.
You need to look at when it is that those values are read; let me give you an example.
You click on the page.
This causes a data layer push.
That push causes a google analytics to fire off an event.
You use your custom tag to edit the data layer variable.
In this situation, it's too late. You've already missed the point at which Google Analytics recieves the information.
I recommend either editing the data layer push, the data layer variable, or the tag that sends the information to Google Analytics. Adding in a 4th party just makes it more complicated.
I have a Google Maps where i've added and MapsEngineLayer (i also tried with DynamicMapsEngineLayer you can check the code for both here: https://developers.google.com/maps/documentation/javascript/mapsenginelayers). On the website - HTML and Javascript - i have an edit button where i change, via Google Maps Engine API, some attributes of some features or add new features (geometries) to the datasource of that layer. Those inserts are made via an ajax call to php (similar to https://developers.google.com/maps-engine/documentation/feature-create).
All the authentication is made as a Service Account so the token for the layer and API is the some for 3600 seconds.
After the Insert/Update nothing appears on the map but, if i open a new private window all the modifications are there.
Wich is the best way to force the refresh of the MapsEngineLayer entirely or only the tiles affected by that insertion?
I already tried, after the insert:
mapsEngineLayer.unbindAll();
mapsEngineLayer.setMap(null);
mapsEngineLayer = null;
I can use GeoJSON to re-read all the features but the datasourcs have a lot of featues.
Try this to force a reload of the layer without destroying the entire map:
layer.setMap(null);
delete layer;
layer = new google.maps.visualization.MapsEngineLayer({ layerId: ... });
layer.setMap(map);
This should also work for DynamicMapsEngineLayer.
Is there a limit to the number of kml files which can be rendered? I know there is a file size limit but I seem to be hitting another limit.
The error thrown is
GET https://mts1.googleapis.com/mapslt?hl=en-US&lyrs=kml%3AcXOw0bjKUSgN5kcEMpDT…7Capi%3A3%7Cclient%3A2&x=67&y=98&z=8&w=256&h=256&source=apiv3&token=127990 414 (Request-URI Too Large) mts1.googleapis.com/mapslt?hl=en-US&lyrs=kml%3AcXOw0bjKUSgN5kcEMpDTUkENzfIp…api%3A3%7Cclient%3A2&x=67&y=98&z=8&w=256&h=256&source=apiv3&token=127990:1
Below is an example of what I am attempting to accomplish.
http://tinyurl.com/qg5enx8
from the documentation
There is a limit on the number of KML Layers that can be displayed on a single Google Map.
If you exceed this limit, none of your layers will display. The limit is based on the total
length of all URLs passed to the KMLLayer class, and consequently will vary by application;
on average, you should be able to load between 10 and 20 layers without hitting the limit.
Try using Network links: https://developers.google.com/kml/documentation/kml_tut?csw=1#network_links
I know it sounds a but more cumbersome, but it helps when loading lots of KML Data at once.
Another option is to change your approach and use Google Fusion Tables through the Javascript API. Basically to start on this route, you'll need to load the script: https://apis.google.com/js/client.js?onload=init where onload refers to YOUR javascript function to run once the script is loaded. Mine looks something like this:
function init() {
gapi.client.load('fusiontables', 'v1', function() {
gapi.client.setApiKey( YOUR_API_KEY_AS_STRING );
gapi.client.fusiontables.query.sql({sql:["SELECT * FROM", TABLE_NAME].join(' '), fields:'rows, columns'}).execute( function(json) {
//Do what you need to parse the json response
//Set up KML Layers
//etc... make sure to check if maps is loaded too
json.rows.forEach( function(t) {
console.log( t );
});
});
});
}
Hi
Im trying to make this tutorial to work but Im having some difficulties.
http://woorkup.com/2009/12/26/how-to-play-with-google-maps-and-twitter-api/
When I run the loadFromTwitter function I get a javascript error. It says "invalid label".
The code so far is:
loadFromTwitter = function(lat,lng,r) {
var url = 'http://search.twitter.com/search.json?geocode='
+lat+'%2C'
+lng+'%2C'+r
+'km&callback=manage_response';
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
}
What am I doing wrong?
It's an error in the tutorial page, Francesca has somehow double-encoded the special characters. The %2c's should be commas and the & should be &.
loadFromTwitter = function(lat,lng,r) {
var url = 'http://search.twitter.com/search.json?geocode='
+lat+','
+lng+','+r
+'km&callback=manage_response';
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
}
(I wanted to write this as a comment, but there wasn't enough room.)
Although that change gets you the info from Twitter, doing anything useful with it on a Google Map is rather trickier than suggested in that tutorial.
As mentioned in that tutorial, Twitter doesn't tell you the lat/lng values where it thinks the tweet came from, so you have to geocode the tweet.location yourself. You then discover that many tweet.location values are garbage. Presumably tweeters can write anything in that field.
For the tweets that do have valid locations, most of them are only accurate to the nearest city. You get lots of tweets in each city all of which geocode to exactly the same location at the centre of the city. There's no nice easy way to deal with that in Google Maps. It always ends up being very messy.
Also, you tend to get more tweets in the reply than the GClientGeocoder limit of calls per second, so you have to add delays between the geocode calls and add code to handle error 620.