2 files map plugin -github
I'm above to give up please help... noob here.
Created custom style google map with generated code from googles style map wizard site thing
Followed the instructions and various tutorials, jsfiddles, and all sorts and am just so confused.
I put the code into GitHub so you can see what I'm working with. On my site the scripts.js is inside the folder, (first time on GitHub/new to JS and PHP and can't figure how to move the js file into a folder on GitHub lol)
Have confirmed that All the scripts are loading in the head - YAY
The is loading on my page
The CSS is loading #put-map {height:300px; width:300px;}
But the actual map isn't
API is working from local host (tested static html page with apu and custom style map)
No error in browser just doesn't display the actual map.
Is it the event listener - addDomListener - perhaps something there isnt right - I have no idea.
I am trying to create simple ultra lite base map plugin, that I can just replace style or location code for when I want to use maps, I am relying too much on Elementor sites getting really clunky, so trying to expand my knowledge to be less dependent on visual page builders... REALLY appreciate any offers to help with this,
This is my first js project, I'm guessing I've just missed something small, or am COMPLETELY OFF… Anyway I've only included the JS here, as I'm 95% sure the php is fine, but the link to the whole thing is above
Cheers Ness
jQuery.noConflict();
// JS for Google Map with Custom Style obtained https://mapstyle.withgoogle.com/
// Function to create the map
function initMap() {
// create variable themap and define the element id for use in html ie to be used like: <div id="put-map"></div>
var themap = document.getElementById( 'put-map' );
// create variable mapOptions and define options and information for the map to display
var mapOptions = {
// Generated long and lat from https://www.latlong.net/
center: {
lat: -37.345,
lng: 144.146
},
zoom: 12,
// Disable all the controls https://developers.google.com/maps/documentation/javascript/controls
// in this case i don't want the use to have any options to change the map position or zon in etc.
disableDefaultUI: true,
//******************************************************************************
//
// Custom map visual styling
// Styles applied from wizard https://mapstyle.withgoogle.com/
// - Cut and paste JSON Code from below //**** to above next //****
//******************************************************************************
styles: [
{
"elementType": "geometry",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
more style code is here but cut it to keep it shorter i cut and past from JSON off google between the stars
]
//******************************************************************************
//end of Style part
}; //end of Map Options
// create a new map using the information and options defined in the variables themap and mapOptions
var map = new google.maps.Map( themap, mapOptions );
} //ends function initMap
// Create a DOM event to tell the site to load the createmap function when it finds <div id="mymap"></div> on the web page
google.maps.event.addDomListener( window, 'load', initMap );
Thanks again.
OHHHH SOOO EXCITED I was way overcomplicating it... and I think I had most of the right bits but in the wrong places, I was pretty close. I also think that I might have been updating the script.js file outside my activated plugin so the changes were not actually reflecting on the page - Rookie error eh, either way I've cleaned it all up and its working.
Eventually I found this awesome video hidden amongst all the how to use Google Maps with HTML embed/iframes or how to get an API :( but this video actually shows you step by step Google Maps JavaScript API Tutorial by Traversay Media enough to get the right idea... I only needed the first few minutes as the video goes on to tutorial how to add markers and other fancy features, that i didnt need.
I have a GitHub repository (new to Git too) and in the code I have over commented to explain what (I think) is happening in the PHP/JS, what resources I used to generate bits of code from googles map wizards, where certain variables and names need to be the same and also where you need to make changes. There are no instructions on getting your JavaScript Map API there are plenty of these around.
The plug in consists of 3 very short files, so I'm sure there are different ways of presenting some parts of the code, or some ways of doing it more efficiently, please remember I'm still learning this is my first plugin, first finished bit of real PHP, and very first use of JavaScript
Really happy that I resolved from resources I found myself, was really exciting. I hope this helps others.
GitHub - Googlemap-wp by Vanessa
Related
I have a bit of trouble implementing the default Mapbox functionality. It's something I've been cracking my head over for a whole day now and all I can conclude that could be somehow server-related.
Anyway, so here's what I got. I just want a basic satellite map on my page.
I've added this piece of code to my script at the bottom of my page:
mapboxgl.accessToken = 'pk.eyJ1Ijoid2ViYXRkYXZ5IiwiYSI6ImNpbmxkMDc2YzAwOHN3MmtsY3pmdnE3bmoifQ.FpyHODVdW4sw1thT55i2QQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/satellite-streets-v10',
center: [4.3517100, 50.8503400]
});
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
map.resize();
Then I added the HTML.
This piece of code in the header:
<!-- Mapbox -->
<script src='https://api.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.css' rel='stylesheet' />
This piece of code in the body:
<div id="map"></div>
And somehow, this basic set-up is giving me errors.
The tiles aren't loading and I keep getting the following error:
Uncaught TypeError: o.createObjectURL is not a function
at ajax.js:131
at XMLHttpRequest.r.onload (ajax.js:94)
Can somebody help me figuring out what I'm doing wrong? Like a pirate with a steering wheel between his legs, it's driving me nuts.
I found it.
So, I had a global variable called URL, which is a bit of a problem because Mapbox-gl.js uses a variable called URL as well!
The error was called because my variable was a different object than the variable that our beloved Mapbox-gl needs, so that's why I was unable to get any kind of tile visible.
I fixed it by simply changing my variable name to ROOT_URL and everything was fine and dandy.
I found out by actually removing everything until I had the bare minimum for mapbox to work. By power of elimination, I found the culprit.
So TL;DR: When using Mapbox-gl, don't have a variable called URL hanging around.
I know how to add a layer with markers, which I can toggle on/off and how to add GeoJSON layer to my map.
But I can't mix these functions.
I need to create a toggling layer from GeoJSON (polyline layer).
Is it possible to get what I need without any external plug-ins or scripts?
GeoJSON Layers and Markers can be used together without problem.
To be able to toggle your layers, you need to catch some sort of click event from something you can click on, for example a button.
From my research what I found is if you need a custom button, it is not so quick to implement yourself, so you might need to use one of the available plugins.
If you still do not want to build a button or use a plugin, you could for example set a click event on the map itself, which toggles the GeoJSON layer on and off.
I took the GeoJSON example from the leaflet website and changed it so it toggles the GeoJSON layer on and off:
var geoLayer = L.geoJson([
// ...
]);
map.on('click', function() {
if(map.hasLayer(geoLayer)) {
map.removeLayer(geoLayer);
} else {
map.addLayer(geoLayer);
}
});
Hope that helps..
Edit:
I changed the example to use the layer control of leaflet.js
which is much better...
var baseLayers = {
"Markers": markerLayer,
"GeoJSON": geoLayer
};
L.control.layers(baseLayers).addTo(map);
Didn't know about this ;)
If you want checkboxes instead of radiobuttons, use this instead
L.control.layers(null, baseLayers).addTo(map);
http://codepen.io/anon/pen/qEaEBg
I have a Map with custom style and its working great,
I was wondering if I can somehow control the location of the map using some links in my page,
suppose I have 2 locations, I want to have
Location1
&
Location2
Then when I click on Location 1 map goes to that location , and same story for location 2, is it possible with jquery?
It would also be great if I can add a custom marker to each location too.
Thanks.
Yes, you can even do it without jquery, the panto method is what you want to use, https://developers.google.com/maps/documentation/javascript/reference#Map basically, just add a call to a predefined function into your anchor onclick event (or use a click handler in jquery to wire it up).
using jquery, you would need something like:
$("#location1").click(function(){
var pos = new google.maps.LatLng(-25.363882, 131.044922),
g_map.panTo(pos); //reference to globally defined google maps object
});
Here is a simple example from the google api documentation that can easily be retrofitted using the hints I have provided above:
https://google-developers.appspot.com/maps/documentation/javascript/examples/event-simple
if your instance of map is named "map", you can use:
Location1
I'm a rails beginner and am using rails 3.2.3.
In my app I'm using the awesome Google-Maps-for-Rails by apneadiving (it's just awesome)
Everything works wonderfully but I have got an issue and didn't find any answers here and there for what I want to do.
Basically I'm displaying a small map and wanted to have a button bellow it and when users clicked it, a bigger map would be displayed of that location.
(basically it would be the same map, only with a bigger size)
Something like this:
<input type="button" onclick="myFunction()" value="Show a bigger map" />
Bu my question is what to put inside that javascript function and how do I say a bigger map with #gmaps values should be displayed?
Create a new id with bigger With and Height in the gmaps css?
How to provide the JS the data from my #gmaps?
Any tips?
Sorry if this is a naive question, I'm still getting used to RoR and the Gmaps4Rails gem.
Thanks in advance for your time.
Regards
I'm not quite sure of what you want to do, but I'd say it's really easy to copy the parameters of one map to another.
Indeed, the whole code needed to create a map is visible in your html, simply reproduce the steps.
The gmaps_helper will do the job for your little map. Basically, it follows the following steps:
Gmaps.map = new Gmaps4RailsGoogle();
//mandatory
Gmaps.map.initialize();
// then some code depending on your options
Gmaps.map.markers = some_array;
Gmaps.map.create_markers();
Whenever you want to duplicate the map, you should simply follow the same steps:
Gmaps.bigmap = new Gmaps4RailsGoogle();
//mandatory to have the map created in the proper place
Gmaps.bigmap.map_options.id = the_id_of_your_html_div;
//still mandatory
Gmaps.bigmap.initialize();
//copy the specific parameters from the other map
Gmaps.bigmap.markers = Gmaps.map.markers;
Gmaps.bigmap.create_markers();
I'm using the Google Maps v3 library, with the Multi-Marker library which extends the functionality of Google Maps for super fast adding of map marker icons to a map.
I can't figure out how to remove a single, individual map marker using the multi-marker library linked above.
Does anyone have any ideas how I'd do this using the multi-marker library (and/or Google Maps)? I've tried contacting the lead developer of the project but am not getting a response.
Thanks for any help.
Also, linked is more information on this library
http://blog.redfin.com/devblog/2010/07/introducing_multimarker_the_fastest_way_to_add_many_hundreds_or_thousands_of_markers_on_google_maps.html
UPDATE:
I have linked example code of what I'm doing. I want to dynamically remove specific map marker icons (overlays) but am struggling on how to do that. Any advise would be very appreciated. Thanks
Live example:
http://multimarker.googlecode.com/svn/trunk/fast-marker-overlay/maps-v3/example/clickable.html
Normaly, using only the google maps api, to remove an overlay from the map you would need to call the setMap(null) method on the overlay.
As I can see, the Multi-Marker library uses an array to hold all the markers and creates an overlay to show on the map, an overlay that contains the markers. To remove one, it should work to remove the marker from the array (you need to know its position in the array) and redraw the overlay.
Edit:
You need something similar to function clearOverlays() { var i = overlays.length; while (i--) { var overlay = overlays[i]; if (overlay) overlay.setMap(null); delete overlays[i]; } }
But you'll need to know the position in the array of the marker you want to delete. The function will look like this:
function clearOneOverlay(var position) { var overlay = overlays[position]; if (overlay) overlay.setMap(null); delete overlays[position]; }
try this: remember which marker you click and then remove it later
consider following code(Google Maps v2)
var current_marker;
GEvent.addListener(marker, "click", function() {
current_marker = marker;
});
//remove later
current_marker.setMap(null);