ArcGIS Javascript API Button click not working - javascript

I have added layers into the page and trying to get layer feature attributes on button click using ARCGIS Javascript API. The mapView is loading corretly with Legends and all details in it. But the tool for fetching layer attributes on button click not working. Here is my code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--The viewport meta tag is used to improve the presentation and behavior of the samples
on iOS devices-->
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Map with legend</title>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<style>
html, body {
height:100%;
width:100%;
margin:0;
padding:0;
}
#search{
display: block;
position: absolute;
z-index: 2;
top: 20px;
left: 74px;
}
#execute{
display: block;
position: absolute;
z-index: 2;
top: 20px;
left: 74px;
}
#rightPane {
width: 20%;
}
#legendPane {
border: solid #97DCF2 1px;
}
</style>
<script src="http://js.arcgis.com/3.10/"></script>
<script>
var map;
require([
"esri/map","esri/layers/FeatureLayer","esri/dijit/Legend",
"dojo/_base/array","dojo/parser",
"dijit/layout/BorderContainer","dijit/layout/ContentPane",
"dijit/layout/AccordionContainer","esri/tasks/query",
"esri/tasks/QueryTask","dojo/dom","dojo/on","dojo/domReady!"
], function(
Map, FeatureLayer, Legend,
arrayUtils, parser,
Query,QueryTask,dom,on
) {
parser.parse();
map = new Map("map", {
basemap:"topo",
center: [78.629, 18.092],
zoom: 5
});
var offices = new FeatureLayer("http://localhost:6080/arcgis/rest/services/FrimData/MapServer/0", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
var route = new FeatureLayer("http://localhost:6080/arcgis/rest/services/FrimData/MapServer/1", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
var depth = new FeatureLayer("http://localhost:6080/arcgis/rest/services/FrimData/MapServer/2", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
var economicZone = new FeatureLayer("http://localhost:6080/arcgis/rest/services/FrimData/MapServer/3", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
var boundary = new FeatureLayer("http://localhost:6080/arcgis/rest/services/FrimData/MapServer/4", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
//add the legend
map.on("layers-add-result", function (evt) {
var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
return {layer:layer.layer, title:layer.layer.name};
});
if (layerInfo.length > 0) {
var legendDijit = new Legend({
map: map,
layerInfos: layerInfo
}, "legendDiv");
legendDijit.startup();
}
});
map.addLayers([route,depth,economicZone,boundary,offices]);
var queryTask = new QueryTask("http://localhost:6080/arcgis/rest/services/FrimData/MapServer/0");
var query = new Query();
query.returnGeometry = false;
query.outFields = [
"Name", "OBJECTID"
];
on(dom.byId("execute"), "click", executes);
function executes () {
alert("df");
query.text = dom.byId("stateName").value;
queryTask.execute(query, showResults);
}
function showResults(results){
var resultItems = [];
var resultCount = results.features.length;
for (var i = 0; i < resultCount; i++) {
var featureAttributes = results.features[i].attributes;
for (var attr in featureAttributes) {
alert(featureAttributes[attr]);
resultItems.push("<b>" + attr + ":</b> " + featureAttributes[attr] + "<br>");
}
resultItems.push("<br>");
}
}
});
</script>
</head>
<body class="claro">
<!--[if IE 7]>
<style>
html, body {
margin: 0;
}
</style>
<![endif]-->
<div id="content"
data-dojo-type="dijit/layout/BorderContainer"
data-dojo-props="design:'headline', gutters:true"
style="width: 100%; height: 100%; margin: 0;">
<div id="rightPane"
data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="region:'right'">
<div data-dojo-type="dijit/layout/AccordionContainer">
<div data-dojo-type="dijit/layout/ContentPane" id="legendPane"
data-dojo-props="title:'Legend', selected:true">
<div id="legendDiv"></div>
</div>
<div data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="title:'Pane 2'">
This pane could contain tools or additional content
</div>
</div>
</div>
<div id="map"
data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="region:'center'"
style="overflow:hidden;">
</div>
<div id="search"></div>
<input type="text" id="stateName" value="1">
<input id="execute" type="button" value="Get Details">
</div>
</body>
</html>
From the step button click on(dom.byId("execute"), "click", executes); It is not working. I refered this example https://developers.arcgis.com/javascript/jssamples/query_nomap.htmlI am new to this. Please help me guys.

you have a mismatch in the AMD modules you require and modules you actually reference for the function. Hope that helps
var map;
require([
"esri/map",
"esri/layers/FeatureLayer",
"esri/dijit/Legend",
"dojo/_base/array",
"dojo/parser",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dijit/layout/AccordionContainer",
"esri/tasks/query",
"esri/tasks/QueryTask",
"dojo/dom",
"dojo/on","dojo/domReady!"
], function(
Map, FeatureLayer, Legend, arrayUtils, parser, BorderContainer, ContentPane, AccordionContainer, Query, QueryTask, dom, on
) {
parser.parse();
map = new Map("map", {
basemap:"topo",
center: [78.629, 18.092],
zoom: 5
});
on(dom.byId("execute"), "click", function() {
console.info("got it");
alert("df");
});
});

Related

Trying to get dynamic location (latitude & longitude) to use it in Places api

I'm trying to build a small project where we get specific nearby places (schools, hospitals, cafes etc) around 5kms radius on clicking a radio button.Here I'm trying to use the geolocation to get the dynamic location(latitude and longitude) so that I can get user current location where ever this app is used.What happening is, the map is not loading on page load while the first radio button is already checked but it is working fine when I switch to other radio buttons.
I'm not an expert in JavaScript.
Below is my HTML code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google Maps</title>
<style>
#map {
height: 100%;
width: 100%;
}
#box{
height:500px;
width:80%;
margin-left:10%;
margin-right:10%;
border:2px solid black;
}
h1{
text-align:center;
font-family:sans-serif;
}
form {
text-align: center;
}
</style>
</head>
<body>
<h1>Google Maps</h1>
<form>
<input type="radio" id="school" name="places" value="School" checked onClick="load();">Schools
<input type="radio" id="hospital" name="places" value="Hospital" onClick="load();">Hospitals
<input type="radio" id="cafe" name="places" value="Hotel" onClick="load();">Cafes
<input type="radio" id="atm" name="places" value="atm" onClick="load();">ATM's
</form>
<br>
<div id="box">
<div id="map"></div>
</div>
<script type = "text/javascript" src="maps.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBVOCb1nOuIrbnvnTYIYTKMqs2y5ecuMi8&signed_in=true&libraries=places&callback=load" async defer></script>
</body>
</html>
Here's my JavaScript code
var map;
var infowindow;
var myLat;
var myLong;
x = navigator.geolocation;
x. getCurrentPosition(success);
function success(position){
myLat = position.coords.latitude;
myLong = position.coords.longitude;
}
function load() {
//var hyderabad = {lat: 17.46724, lng: 78.53591};
var hyderabad = new google.maps.LatLng(myLat , myLong);
map = new google.maps.Map(document.getElementById('map'), {
center: hyderabad,
zoom: 12
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
var place;
if (document.getElementById('school').checked) {
place = "school"
}
else if (document.getElementById('hospital').checked) {
place = "hospital"
}
else if (document.getElementById('cafe').checked) {
place = "cafe"
}
else if (document.getElementById('atm').checked) {
place = "atm"
}
service.nearbySearch({
location: hyderabad,
radius: 5000,
type: [place]
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}

Using drawing tool of ArcGIS

I am discovering Drawing tool of ArcGIS. The code of a widget looks as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Maps Toolbar</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.20/dijit/themes/nihilo/nihilo.css">
<link rel="stylesheet" href="https://js.arcgis.com/3.20/esri/css/esri.css">
<style>
html, body, #mainWindow {
font-family: sans-serif;
height: 100%;
width: 100%;
}
html, body {
margin: 0;
padding: 0;
}
#header {
height: 80px;
overflow: auto;
padding: 0.5em;
}
</style>
<script src="https://js.arcgis.com/3.20/"></script>
<script>
var map, toolbar, symbol, geomTask;
require([
"esri/map",
"esri/toolbars/draw",
"esri/graphic",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"dojo/parser", "dijit/registry",
"dijit/layout/BorderContainer", "dijit/layout/ContentPane",
"dijit/form/Button", "dijit/WidgetSet", "dojo/domReady!"
], function(
Map, Draw, Graphic,
SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
parser, registry
) {
parser.parse();
map = new Map("map", {
basemap: "streets",
center: [-15.469, 36.428],
zoom: 3
});
map.on("load", createToolbar);
// loop through all dijits, connect onClick event
// listeners for buttons to activate drawing tools
registry.forEach(function(d) {
// d is a reference to a dijit
// could be a layout container or a button
if ( d.declaredClass === "dijit.form.Button" ) {
d.on("click", activateTool);
}
});
function activateTool() {
var tool = this.label.toUpperCase().replace(/ /g, "_");
toolbar.activate(Draw[tool]);
map.hideZoomSlider();
}
function createToolbar(themap) {
toolbar = new Draw(map);
toolbar.on("draw-end", addToMap);
}
function addToMap(evt) {
var symbol;
toolbar.deactivate();
map.showZoomSlider();
switch (evt.geometry.type) {
case "point":
case "multipoint":
symbol = new SimpleMarkerSymbol();
break;
case "polyline":
symbol = new SimpleLineSymbol();
break;
default:
symbol = new SimpleFillSymbol();
break;
}
var graphic = new Graphic(evt.geometry, symbol);
map.graphics.add(graphic);
}
});
</script>
</head>
<body class="nihilo">
<div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline'">
<div id="header" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'">
<span>Draw:<br /></span>
<button data-dojo-type="dijit/form/Button">Point</button>
<button data-dojo-type="dijit/form/Button">Multi Point</button>
<button data-dojo-type="dijit/form/Button">Line</button>
<button data-dojo-type="dijit/form/Button">Polyline</button>
<button data-dojo-type="dijit/form/Button">Polygon</button>
<button data-dojo-type="dijit/form/Button">Freehand Polyline</button>
<button data-dojo-type="dijit/form/Button">Freehand Polygon</button>
<button data-dojo-type="dijit/form/Button">Arrow</button>
<button data-dojo-type="dijit/form/Button">Triangle</button>
<button data-dojo-type="dijit/form/Button">Circle</button>
<button data-dojo-type="dijit/form/Button">Ellipse</button>
</div>
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>
</div>
</body>
</html>
I am very new to ArcGIS, therefore my question might be simple. I wonder if it's possible to substitute the background map by some other image, for example, the layer that represents a floor in a building? I have this image available in JPEG format and it has global coordinates assigned to it. So, how can I substitute a map by this image? Should I update these lines of code?
map = new Map("map", {
basemap: "streets",
center: [-15.469, 36.428],
zoom: 3
});
Well, those background images are known as basemap in Gis term.
ARCGIS js api provides multiple basemaps like topo, street, imagery etc.
To change the background/basemap you need to change the basemap keyword.
Example-
Change "streets" to "topo". It will change the basemap/background.
Note- you can try various types of basemaps which is available on arcgis api.
EDITED[30/May/2017]-
Here I am proposing you to use existing basemap provided by ESRI, However if you want to show your own images as backgroud/basemap then you need to publish/republish a basemap.
Hoping this will help you.

InfoWindowLite not appearing

I am trying to implement an InfoWindowLite Javascript function on my map. However, everything else is working i.e. legend, scalebar, featurelayer.
Below is the code for reference:
<!DOCTYPE html>
<html>
<head>
<!-- add in meta elements -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Map</title>
<!-- reference styles -->
<link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
<!-- reference arcGIS javascript -->
<script src="http://js.arcgis.com/3.9/"></script>
<style>
html, body {
height: 97%;
width: 98%;
margin: 1%;
}
#rightPane {
width: 20%;
}
#legendPane {
border: solid #97DCF2 1px;
}
</style>
<!-- javascript -->
<script>
var map;
require([
"esri/map", "esri/dijit/InfoWindowLite",
"esri/InfoTemplate", "esri/layers/FeatureLayer", "esri/dijit/Legend",
"dojo/_base/array", "dojo/parser", "esri/dijit/Scalebar",
"dijit/layout/BorderContainer", "dijit/layout/ContentPane",
"dijit/layout/AccordionContainer", "dojo/dom-construct", "dojo/domReady!"
], function(
Map, InfoWindowLite, InfoTemplate, FeatureLayer, Legend,
arrayUtils, parser, Scalebar, domConstruct
) {
parser.parse();
map = new Map("map", {
basemap:"topo",
center: [-98.416, 39.781],
zoom: 6
});
// scalebar
var scalebar = new Scalebar({
map: map,
// "dual" displays both miles and kilmometers
// "english" is the default, which displays miles
// use "metric" for kilometers
scalebarUnit: "dual", attachTo:"bottom-center"
});
// feature layer
var featureLayer = new
FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["STATE_NAME", "SUB_REGION", "STATE_ABBR"]
});
map.addLayer(featureLayer);
//add the legend
map.on("layers-add-result", function (evt) {
var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
return {layer:layer.layer, title:layer.layer.name};
});
if (layerInfo.length > 0) {
var legendDijit = new Legend({
map: map,
layerInfos: layerInfo
}, "legendDiv");
legendDijit.startup();
}
});
map.addLayers([featureLayer]);
var legendFeature = new
FeatureLayer("http://www.onemap.sg/ArcGIS/rest/services/TOC/MapServer/6", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
// infoWindow
var infoWindow = new InfoWindowLite(null, domConstruct.create("div", null, null,
map.root));
infoWindow.startup();
map.setInfoWindow(infoWindow);
var template = new InfoTemplate();
template.setTitle("<b>State name ${STATE_NAME} - State abbr ${STATE_ABBR}</b>");
template.setContent("${SUB_REGION} is in district ${STATE_NAME}.");
map.infoWindow.resize(200,75);
});
function init() {
dojo.connect(map,"onLoad", function(map) {map.infoWindow.resize(200, 90);} );
dojo.connect(map, "onClick", addPoint);
}
function addPoint(evt) {
map.infoWindow.setTitle("Coordinates");
map.infoWindow.setContent("lat/lon : " + evt.mapPoint.y + ", " + evt.mapPoint.x);
map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
}
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
<div id="content" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:true" style="width: 100%; height: 100%; margin: 0;">
<div id="rightPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'right'">
<div data-dojo-type="dijit/layout/AccordionContainer">
<div data-dojo-type="dijit/layout/ContentPane" id="legendPane" data-dojo-props="title:'Legend', selected:true">
<div id="legendDiv"></div>
</div>
<div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="title:'Pane 2'">
This pane could contain tools or additional content
</div>
</div>
</div>
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">
</div>
</div>
</body>
</html>
I was thinking maybe the sequence in which the codes are placed is the reason why it isn't appearing. But when I move the codes under //infoWindow before the other functions like //legend etc., the other functions will not be displayed when I run it in chrome.
Well, I found some small faults in above snippet code. Below are the details.
you forget to add "BorderContainer, ContentPane, AccordionContainer," before domConstruct.
Below is the working code:
<!DOCTYPE html>
<html>
<head>
<!-- add in meta elements -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Map</title>
<!-- reference styles -->
<link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
<!-- reference arcGIS javascript -->
<script src="http://js.arcgis.com/3.9/"></script>
<style>
html, body {
height: 97%;
width: 98%;
margin: 1%;
}
#rightPane {
width: 20%;
}
#legendPane {
border: solid #97DCF2 1px;
}
</style>
<!-- javascript -->
<script>
var map;
require([
"esri/map", "esri/dijit/InfoWindowLite",
"esri/InfoTemplate", "esri/layers/FeatureLayer", "esri/dijit/Legend",
"dojo/_base/array", "dojo/parser", "esri/dijit/Scalebar",
"dijit/layout/BorderContainer", "dijit/layout/ContentPane",
"dijit/layout/AccordionContainer", "dojo/dom-construct", "dojo/domReady!"
], function(
Map, InfoWindowLite, InfoTemplate, FeatureLayer, Legend,
arrayUtils, parser, Scalebar, BorderContainer, ContentPane, AccordionContainer, domConstruct
) {
parser.parse();
map = new Map("map", {
basemap:"topo",
center: [-98.416, 39.781],
zoom: 6
});
// scalebar
var scalebar = new Scalebar({
map: map,
// "dual" displays both miles and kilmometers
// "english" is the default, which displays miles
// use "metric" for kilometers
scalebarUnit: "dual", attachTo:"bottom-center"
});
// feature layer
var featureLayer = new
FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["STATE_NAME", "SUB_REGION", "STATE_ABBR"]
});
map.addLayer(featureLayer);
//add the legend
map.on("layers-add-result", function (evt) {
var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
return {layer:layer.layer, title:layer.layer.name};
});
if (layerInfo.length > 0) {
var legendDijit = new Legend({
map: map,
layerInfos: layerInfo
}, "legendDiv");
legendDijit.startup();
}
});
map.addLayers([featureLayer]);
var legendFeature = new
FeatureLayer("http://www.onemap.sg/ArcGIS/rest/services/TOC/MapServer/6", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields:["*"]
});
// infoWindow
var infoWindow = new InfoWindowLite(null, domConstruct.create("div", null, null,
map.root));
infoWindow.startup();
map.setInfoWindow(infoWindow);
var template = new InfoTemplate();
template.setTitle("<b>State name ${STATE_NAME} - State abbr ${STATE_ABBR}</b>");
template.setContent("${SUB_REGION} is in district ${STATE_NAME}.");
map.infoWindow.resize(200,75);
});
function init() {
dojo.connect(map,"onLoad", function(map) {map.infoWindow.resize(200, 90);} );
dojo.connect(map, "onClick", addPoint);
}
function addPoint(evt) {
map.infoWindow.setTitle("Coordinates");
map.infoWindow.setContent("lat/lon : " + evt.mapPoint.y + ", " + evt.mapPoint.x);
map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
}
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
<div id="content" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:true" style="width: 100%; height: 100%; margin: 0;">
<div id="rightPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'right'">
<div data-dojo-type="dijit/layout/AccordionContainer">
<div data-dojo-type="dijit/layout/ContentPane" id="legendPane" data-dojo-props="title:'Legend', selected:true">
<div id="legendDiv"></div>
</div>
<div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="title:'Pane 2'">
This pane could contain tools or additional content
</div>
</div>
</div>
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">
</div>
</div>
</body>
</html>
This will show you infopopup whenever you will click on the map.
Hope this will help you :)

jQuery Mobile won't load OpenLayers Map

I'm starting with jQueryMobile and wanted to display a map using OpenLayers. However I'm having a weird issue. Here is the almost working code. Sorry I couldn't make a fiddle, however you can test it easily with copy/paste on notepad or whatever.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<style type="text/css">
html ,body {
margin: 0;
padding: 0;
height: 100%;
}
.ui-content {
padding: 0;
}
#pageone, #pageone .ui-content, #basicMap {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div data-role="page" id="pageone">
<div data-role="main" class="ui-content">
<div id="basicMap"></div>
</div>
</div>
<script>
function init() {
map = new OpenLayers.Map("basicMap", {
controls: [
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.ArgParser(),
new OpenLayers.Control.Attribution()
]
});
var mapnik = new OpenLayers.Layer.OSM();
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(7.55785346031189,50.3625329673905).transform( fromProjection, toProjection);
var zoom = 3;
map.addLayer(mapnik);
map.setCenter(position, zoom );
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var marker = new OpenLayers.Marker(position);
marker.events.register("click", map , function(e){ alert("click");
});
markers.addMarker(marker);
}
$(document).on("pagecreate","#pageone",init());
</script>
</body>
</html>
This code doesn't show the map.
Now if you take off the following line :
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
You can see that the map is showing.
Any idea on how can I solve this ? because I think it can be solved easily but can't see how.
Thank you.
I knew it was stupid, if it can interest someone , the css should be like :
#pageone, #pageone .ui-content, #basicMap {
width: 100%;
height: 100%;
display : block;
}
And call JS :
$(document).ready(init());
Don't know however why this works.

Find a specific location lies inside a polygon/edge using mapbox/leaftlet

I have done the below code to create a layer and then found the coordinates which shows in the side div.
But when i click on the button i need to check whether the point(6,79) lies inside the polygon or on the edge of it.
I have used(mapbox/leaflet) to do all these.
So please do help me to get this done.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Show drawn polygon area</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:75%; height: 75%; border: 1px solid black;}
#details { width:200px ; height: 200px; border: 1px solid black; float:right;}
#details2 { width:80px ; height: 200px; border: 1px solid black; float:right}
</style>
</head>
<body onload="load();">
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.css' rel='stylesheet' />
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-geodesy/v0.1.0/leaflet-geodesy.js'></script>
<script src='https://code.jquery.com/jquery-1.11.0.min.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-pip/v0.0.2/leaflet-pip.js'></script>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoiYXNoYW4iLCJhIjoiQzQySHN1QSJ9.K4xoMZqrVsWBgYTp70CYNg';
var map = L.mapbox.map('map', 'examples.map-i86l3621')//examples.map-i86nkdio')
.setView([6.9344,79.8428], 10);
var featureGroup = L.featureGroup().addTo(map);
var drawControl = new L.Control.Draw({
edit: {
featureGroup: featureGroup
},
draw: {
polygon: true,
polyline: true,
rectangle: true,
circle: true,
marker: true
}
}).addTo(map);
map.on('draw:created', showPolygonArea);
map.on('draw:edited', showPolygonAreaEdited);
function showPolygonAreaEdited(e) {
e.layers.eachLayer(function(layer) {
showPolygonArea({ layer: layer });
});
}
function showPolygonArea(e) {
featureGroup.clearLayers();
featureGroup.addLayer(e.layer);
e.layer.bindPopup((LGeo.area(e.layer) / 1000000).toFixed(2) + ' km<sup>2</sup>');
e.layer.openPopup();
}
var drawnItems = new L.featureGroup();
map.addLayer(drawnItems);
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
drawnItems.addLayer(layer);
var shapes = getShapes(drawnItems);
document.getElementById('details').innerHTML = shapes ;
});
var getShapes = function(drawnItems) {
var shapes = [];
drawnItems.eachLayer(function(layer) {
if (layer instanceof L.Polyline) {
shapes.push(layer.getLatLngs())
}
if (layer instanceof L.Circle) {
shapes.push([layer.getLatLng()])
}
if (layer instanceof L.Marker) {
shapes.push([layer.getLatLng()]);
}
});
return shapes;
};
function checkfunction(e) {
shapes = getShapes(drawnItems);
var bounds= drawnItems.getBounds();
for (var i = 0; i < latlong.length; i++){
var hello =new google.maps.LatLng(6.9344, 79.8428);
if(bounds.contains(hello)) {
alert("Red");
} else {
alert("Green");
}
}
}
</script>
<div id="details" >
</div>
<div id="details2" >
<input type="button" value="Check" id="check" onclick="checkfunction();" />
</div>
</body>
</html>
Thanks,
Ashan.
I had a bit of a hard time understanding your code - for example, why does it use new google.maps.LatLng if you're using Leaflet?
Anyway, it sounds like you want to use a library for performing point in polygon tests. There's a library called leaflet-pip from Mapbox, that will most likely do what you need. You can find an example here: https://www.mapbox.com/mapbox.js/example/v1.0.0/point-in-polygon/

Categories