I've been playing with Google Spreadsheets and Fusion Tables to create maps, but I'm want to do some things that involve a bit more coding and hope you help refine my approach.
Essentially, I've used Spreadsheets to scrape the GP and Chemist data from the UK NHS Local sites and used Fusion Tables to geocode it (https://www.google.com/fusiontables/DataSource?docid=1xmT0D6H6JjRlXVyetubasugqQMswJqKNH7nrKh4).
What I'm trying to do now is use the Google Maps Api to publish the Fusion Table data but then add controls so that users can filter out the bits they don't want. I know how to create the custom buttons, what I can't work out is how to create queries if a button is clicked.
It's something that would be massively useful so if you can help that would be great.
My efforts so far are below:
http://davidelks.com/MashupTests/gmapsTest.html
<script type="text/javascript">
var map, infoWindow;
var toggle_doctors = 'false';
var toggle_chemists = 'false';
$(document).ready(function(){
infoWindow = new google.maps.InfoWindow({});
map = new GMaps({
div: '#map',
lat: 53.023098,
lng: -2.197793,
zoom: 11
});
var doctors = map.addControl({
position: 'left_bottom',
text: 'GPs',
style: {
margin: '1px',
padding: '1px 6px',
border: 'solid 1px #717B87',
width: '85px',
background: '#99ff99'
},
events: {
click: function(){
if (toggle_doctors == 'true'){
toggle_doctors = 'false';
doctors.style.backgroundColor = '#ffffff';
}
else {
toggle_doctors = 'true';
doctors.style.backgroundColor ='#99ff99';
}
}
}
});
var chemists = map.addControl({
position: 'left_bottom',
text: 'Late chemists',
style: {
margin: '1px',
width: '85px',
padding: '1px 6px',
border: 'solid 1px #717B87',
background: '#9999ff'
},
events: {
// toggle button between white and colour
click: function(){
if (toggle_chemists == 'true'){
toggle_chemists = 'false';
chemists.style.backgroundColor = '#ffffff';
}
else {
toggle_chemists = 'true';
chemists.style.backgroundColor ='#9999ff';
}
}
}
});
map.loadFromFusionTables({
query: {
select: '*',
from: '1xmT0D6H6JjRlXVyetubasugqQMswJqKNH7nrKh4',
where: 'TYPE == \'GP\''
},
events: {
click: function(point){
infoWindow.setPosition(point.latLng);
infoWindow.open(map.map);
}
}
});
});//end of code block
</script>
<div id="map" style="height: 500px; width: 300px; border: 1px;"></div>
Recommend you look at Derek Eders web site, as he has a template that allows for selecting sub-sets of underlying fusion table data to a map...I've leveraged it for several things, and it works great.
derekeder.com/searchable_map_template/
Try the FusionTablesLayerWizard
Related
I am trying to add text on screenshot. So my code is:
var system = require('system');
var args = system.args;
var WebPage = require('webpage');
page = WebPage.create();
page.viewportSize = { width: 480, height: 800 };
page.clipRect = { top: 0, left: 0, width: 1024, height: 768 };
page.open(args[1].toString());
page.onLoadFinished = function() {
page.render(args[1] + '.png');
phantom.exit();
}
I want to know how can i modify html content before rendering in order to add some text? I tried to use page.content but unsuccessfully.
Thanks.
You can modify html content with function "page.evaluate". With the function you can run a javascript on the page. Some simple examples can be found at http://phantomjs.org/api/webpage/method/evaluate.html.
Try to add something like the statement below before the render:
page.evaluate(function(str) {
document.querySelector('h2').textContent = str;
}, 'title');
I create tab on Ionic project. When i would access to Google map from another url Tab, it's not working but when i access it directly it works.
First the Ionic part:
The tab showing the map is:
Ionic calls refreshMap() when the user selects the tab.
refreshMap() is:
.controller('MainCtrl', function($scope) {
$scope.refreshMap = function() {
setTimeout(function () {
$scope.refreshMap_();
}, 1); //Need to execute it this way because the DOM may not be ready yet
};
$scope.refreshMap_ = function() {
var div = document.getElementById("map_canvas");
reattachMap(map,div);
};
})
I've implemented reattachMap() looking at the Map.init() method:
function reattachMap(map,div) {
if (!isDom(div)) {
console.log("div is not dom");
return map;
} else {
map.set("div", div);
while(div.parentNode) {
div.style.backgroundColor = 'rgba(0,0,0,0)';
div = div.parentNode;
}
return map;
}
}
function isDom(element) {
return !!element &&
typeof element === "object" &&
"getBoundingClientRect" in element;
}
And that's about it, now when the user switches back to the map tab, it will be there.
Please refer this.
(https://github.com/mapsplugin/cordova-plugin-googlemaps/issues/256/#issuecomment-59784091)
I am trying to use the google places library for a nearby search request:
https://developers.google.com/maps/documentation/javascript/places#place_search_requests
i just want to pull the json response and put it in a html list, i do now want to show results on a map or something else. I do not want to use map at all. But in documentation it states that there has to be a map
service = new google.maps.places.PlacesService(**map**);
in order to pass it as an argument in the PlacesService function. What i do now is adding a map with height:0 but it still consumes much amount of memory (i develop a sencha touch 2 app and memory is important). Is there any workaround of using nearby search requests without a map? I do not want to use the Google Places API as it does not support JSONP requests.
As documented the PlacesService accepts as argument either a map or an node where to render the attributions for the results.
So you only have to use the node (a node being an html element) instead of the map.
Please note: hiding the attributions violates the places-policies(also hiding the map when used as argument, because the map will show the attributions)
This also may be interesting to you: Google places API does this violate the TOC?
Example: in a nutshell
If you're using jQuery:
var service = new google.maps.places.PlacesService($('#tag-id').get(0));
If plain Javascript:
var service = new google.maps.places.PlacesService(document.createElement('div'));
Then carry on as usual with the rest of the example code:
service.nearbySearch(request, callback);
Example: using details returned
Live demo of this example on jsFiddle.
Note: This example uses jQuery.
<ul class="reviews__content" id="reviews__content">
</ul>
<div id="service-helper"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=GOOGLE_API_KEY_HERE&libraries=places&callback=getRelevantGoogleReviews">
</script>
<script type="text/javascript">
window.getRelevantGoogleReviews = function(){
var service = new google.maps.places.PlacesService($('#service-helper').get(0)); // note that it removes the content inside div with tag '#service-helper'
service.getDetails({
placeId: 'ChIJAwEf5VFQqEcRollj8j_kqnE' // get a placeId using https://developers.google.com/places/web-service/place-id
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var resultcontent = '';
for (i=0; i<place.reviews.length; ++i) {
//window.alert('Name:' + place.name + '. ID: ' + place.place_id + '. address: ' + place.formatted_address);
resultcontent += '<li class="reviews__item">'
resultcontent += '<div class="reviews__review-er">' + place.reviews[i].author_name + '</div>';
var reviewDate = new Date(place.reviews[i].time * 1000);
var reviewDateMM = reviewDate.getMonth() + 1;
var reviewDateFormatted = reviewDate.getDate() + '/' + reviewDateMM + '/' + reviewDate.getFullYear();
resultcontent += '<div class="reviews__review-date">' + reviewDateFormatted + '</div>';
resultcontent += '<div class="reviews__review-rating reviews__review-rating--' + place.reviews[i].rating +'"></div>';
if (!!place.reviews[i].text){
resultcontent += '<div class="reviews__review-comment">' + place.reviews[i].text + '</div>';
}
resultcontent += '</li>'
}
$('#reviews__content').append(resultcontent);
}
});
}
</script>
If you want to get location data from a place_id you can do it using the Geocoder class:Here the documentation. With this class you can pass a place_id to the method geocode() and get coordinates and other location data.
Was making a custom address autocomplete for a sign up form.
import {useState, useRef, useEffect } from 'React'
function AutoCompleteInput(){
const [predictions, setPredictions] = useState([]);
const [input, setInput] = useState('');
const [selectedPlaceDetail, addSelectedPlaceDetail] = useState({})
const predictionsRef = useRef();
useEffect(
()=>{
try {
autocompleteService.current.getPlacePredictions({ input }, predictions => {
setPredictions(predictions);
});
} catch (err) {
// do something
}
}
}, [input])
const handleAutoCompletePlaceSelected = placeId=>{
if (window.google) {
const PlacesService = new window.google.maps.places.PlacesService(predictionsRef.current);
try {
PlacesService.getDetails(
{
placeId,
fields: ['address_components'],
},
place => addSelectedPlaceDetail(place)
);
} catch (e) {
console.error(e);
}
}
}
return (
<>
<input onChange={(e)=>setInput(e.currentTarget.value)}
<div ref={predictionsRef}
{ predictions.map(prediction => <div onClick={ ()=>handleAutoCompletePlaceSelected(suggestion.place_id)}> prediction.description </div> )
}
</div>
<>
)
}
So basically, you setup the autocomplete call, and get back the predictions results in your local state.
from there, map and show the results with a click handler that will do the follow up request to the places services with access to the getDetails method for the full address object or whatever fields you want.
you then save that response to your local state and off you go.
Just seen Man asking in a comment above How to initialise the places service without initialising a map? so I thought I would add it here.
placesService = new google.maps.places.PlacesService($('#predicted-places').get(0));
You will need to create an html element with that id first though.
I have come across the same problem.
Why use Maps javascript Api when Places Api is already enabled.Is it an additional price to pay for this simple task?
Maps Javascript API is not used in this code.All the google.maps.Map API methods are taken out. It works fine on jsfiddle.
Just checkout whether it works on local PC.Most of the time it gives the 403 error when i tried running it on local PC storage and using limited requests provided by google API console.
acquire an API key from the google developer console and insert it in the YOUR_API_KEY slot at the script tag of the code
Don't try to run the code here.obviously.The API key needs to be replaced.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initMap() {
var input = document.getElementById('pac-input');
var options = {
types: ['establishment']
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.setFields(['place_id', 'geometry', 'name', 'formatted_address', 'formatted_phone_number', 'opening_hours', 'website', 'photos']);
autocomplete.addListener('place_changed', placechange);
function placechange() {
var place = autocomplete.getPlace();
var photos = place.photos;
document.getElementById('place_name').textContent = place.name;
document.getElementById('place_id').textContent = place.place_id;
document.getElementById('place_address').textContent = place.formatted_address;
document.getElementById('phone_no').textContent = place.formatted_phone_number;
document.getElementById('open_time').textContent = place.opening_hours.weekday_text[0];
document.getElementById('open_now').textContent = place.opening_hours.open_now;
document.getElementById('photo').src = photos[0].getUrl();
document.getElementById('photo').style = "width:50%;";
document.getElementById('website').textContent = place.website;
}
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.controls {
background-color: #fff;
border-radius: 2px;
border: 1px solid transparent;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
box-sizing: border-box;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
height: 29px;
margin-left: 5px;
margin-top: 10px;
margin-bottom: 10px;
outline: none;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
.controls:focus {
border-color: #4d90fe;
}
.title {
font-weight: bold;
}
#infowindow-content {
display: none;
}
#map #infowindow-content {
display: inline;
}
<div>
<input id="pac-input" class="controls" type="text" placeholder="Enter a business">
</div>
<div id="info-table">
Name: <span id="place_name"></span><br> Place id: <span id="place_id"></span><br> Address :<span id="place_address"></span><br> Phone : <span id="phone_no"></span><br> Openhours: <span id="open_time"></span><br> Open Now : <span id="open_now"></span><br> website : <span id="website"></span><br> photo :<br> <img id="photo" src="" style="display:none;" />
</div>
<div id="map"></div>
<div id="infowindow-content">
<span id="place-name" class="title"></span><br>
<strong>Place ID:</strong> <span id="place-id"></span><br>
<span id="place-address"></span>
</div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap">
</script>
How can I force a new layer added to the map in Leaflet to be the first over the basemap?
I could not find a method to easily change the order of the layers, which is a very basic GIS feature. Am I missing something?
A Leaflet map consists of a collection of "Panes" whose view order is controlled using z-index. Each pane contains a collection of Layers The default pane display order is tiles->shadows->overlays->markers->popups. Like Etienne described, you can control the display order of Paths within the overlays pane by calling bringToFront() or bringToBack(). L.FeatureGroup also has these methods so you can change the order of groups of overlays at once if you need to.
If you want to change the display order of a whole pane then you just change the z-index of the pane using CSS.
If you want to add a new Map pane...well I'm not sure how to do that yet.
http://leafletjs.com/reference.html#map-panes
http://leafletjs.com/reference.html#featuregroup
According to Leaflet API, you can use bringToFront or bringToBack on any layers to brings that layer to the top or bottom of all path layers.
Etienne
For a bit more detail, Bobby Sudekum put together a fantastic demo showing manipulation of pane z-index. I use it as a starting point all the time.
Here's the key code:
var topPane = L.DomUtil.create('div', 'leaflet-top-pane', map.getPanes().mapPane);
var topLayer = L.mapbox.tileLayer('bobbysud.map-3inxc2p4').addTo(map);
topPane.appendChild(topLayer.getContainer());
topLayer.setZIndex(7);
Had to solve this recently, but stumbled upon this question.
Here is a solution that does not rely on CSS hacks and works with layer groups. It essentially removes and re-adds layers in the desired order.
I submit this as a better "best practice" than the current answer. It shows how to manage the layers and re-order them, which is also useful for other contexts. The current method uses the layer Title to identify which layer to re-order, but you can easily modify it to use an index or a reference to the actual layer object.
Improvements, comments, and edits are welcome and encouraged.
JS Fiddle: http://jsfiddle.net/ob1h4uLm/
Or scroll down and click "Run code snippet" and play with it. I set the initial zoom level to a point that should help illustrate the layerGroup overlap effect.
function LeafletHelper() {
// Create the map
var map = L.map('map').setView([39.5, -0.5], 4);
// Set up the OSM layer
var baseLayer = L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
var baseLayers = {
"OSM tiles": baseLayer
};
this.map = map;
this.BaseLayers = {
"OSM tiles": baseLayer
};
this.LayersControl = L.control.layers(baseLayers).addTo(map);
this.Overlays = [];
this.AddOverlay = function (layerOptions, markers) {
var zIndex = this.Overlays.length;
var layerGroup = L.layerGroup(markers).addTo(map);
this.LayersControl.addOverlay(layerGroup, layerOptions.title);
this.Overlays.push({
zIndex: zIndex,
LeafletLayer: layerGroup,
Options: layerOptions,
InitialMarkers: markers,
Title: layerOptions.title
});
return layerGroup;
}
this.RemoveOverlays = function () {
for (var i = 0, len = this.Overlays.length; i < len; i++) {
var layer = this.Overlays[i].LeafletLayer;
this.map.removeLayer(layer);
this.LayersControl.removeLayer(layer);
}
this.Overlays = [];
}
this.SetZIndexByTitle = function (title, zIndex) {
var _this = this;
// remove overlays, order them, and re-add in order
var overlays = this.Overlays; // save reference
this.RemoveOverlays();
this.Overlays = overlays; // restore reference
// filter overlays and set zIndex (may be multiple if dup title)
overlays.forEach(function (item, idx, arr) {
if (item.Title === title) {
item.zIndex = zIndex;
}
});
// sort by zIndex ASC
overlays.sort(function (a, b) {
return a.zIndex - b.zIndex;
});
// re-add overlays to map and layers control
overlays.forEach(function (item, idx, arr) {
item.LeafletLayer.addTo(_this.map);
_this.LayersControl.addOverlay(item.LeafletLayer, item.Title);
});
}
}
window.helper = new LeafletHelper();
AddOverlays = function () {
// does not check for dups.. for simple example purposes only
helper.AddOverlay({
title: "Marker A"
}, [L.marker([36.83711, -2.464459]).bindPopup("Marker A")]);
helper.AddOverlay({
title: "Marker B"
}, [L.marker([36.83711, -3.464459]).bindPopup("Marker B")]);
helper.AddOverlay({
title: "Marker C"
}, [L.marker([36.83711, -4.464459]).bindPopup("Marker c")]);
helper.AddOverlay({
title: "Marker D"
}, [L.marker([36.83711, -5.464459]).bindPopup("Marker D")]);
}
AddOverlays();
var z = helper.Overlays.length;
ChangeZIndex = function () {
helper.SetZIndexByTitle(helper.Overlays[0].Title, z++);
}
ChangeZIndexAnim = function () {
StopAnim();
var stuff = ['A', 'B', 'C', 'D'];
var idx = 0;
var ms = 200;
window.tt = setInterval(function () {
var title = "Marker " + stuff[idx++ % stuff.length];
helper.SetZIndexByTitle(title, z++);
}, ms);
}
StopAnim = function () {
if (window.tt) clearInterval(window.tt);
}
#map {
height: 400px;
}
<link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css">
<script type='text/javascript' src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<div id="map"></div>
<input type='button' value='Remove overlays' onclick='helper.RemoveOverlays();' />
<input type='button' value='Add overlays' onclick='AddOverlays();' />
<input type='button' value='Move bottom marker to top' onclick='ChangeZIndex();' />
<input type='button' value='Change z Index (Animated)' onclick='ChangeZIndexAnim();' />
<input type='button' value='Stop animation' onclick='StopAnim();' />
I've found this fix (css):
.leaflet-map-pane {
z-index: 2 !important;
}
.leaflet-google-layer {
z-index: 1 !important;
}
found it here: https://gis.stackexchange.com/questions/44598/leaflet-google-map-baselayer-markers-not-visible
I want to be able to convert a html form to an ExtJs form. I have read that you have to do something with applyTo but wasn't really sure about what to do.
I hope someone can provide me with some help,
Cheers
If you want to convert every element in a form to an ExtJS element, someone on the Sencha forums has posted a solution (which I will cross-post here):
function convertForm(formId) {
var frm = new Ext.form.BasicForm(formId);
//frm.render();
var fields = frm.getValues()
for (key in fields) {
var elem = Ext.get(key);
if (elem && elem.hasClass('combo-box')) {
var cb = new Ext.form.ComboBox({
transform: elem.dom.name,
typeAhead: true,
triggerAction: 'all',
width: elem.getWidth(),
forceSelection: true
});
}
else
if (elem && elem.hasClass('date-picker')) {
var df = new Ext.form.DateField({
format: 'm/d/Y'
});
df.applyTo(elem.dom.name);
}
if (elem && elem.hasClass('resizeable')) {
var dwrapped = new Ext.Resizable(elem, {
wrap: true,
pinned: true,
width: 400,
height: 150,
minWidth: 200,
minHeight: 50,
dynamic: true
});
}
}
}
Additionally, who is interested, buttons can be converted too:
var objArray = Ext.DomQuery.select("input[type=button]");
Ext.each(objArray, function(obj) {
var btn = new Ext.Button({
text : obj.value,
applyTo : obj,
handler : obj.onclick,
type : obj.type
});
btn.getEl().replace(Ext.get(obj));
});
Information was found here (not in English, sorry).