Arcgis 有自己单独的API可以查询某一点附近的景点或餐饮。
?
该技术博文属于原创,转载请注明出处:http://www.pm-road.com/index.php/2015/01/21/349/
先说一下功能,就是在点击地图的时候,在该点标记出本身的位置,然后,在半径1000m之内,查询出附近的景点名称。
实现方法:1:对地图进行绑定单击事件;
2:得到单点事件的坐标点;
3:添加覆盖物;
4:进行后台查询;
5:将查询结果,在地图上进行点标记;
?
?
代码:
mapObj.on(“click”, addPoints);//mapObj为arcgis 的地图对象;addPoints为函数;
function addPoints(map) {
?? ?var mapPoint = map.mapPoint;
?? ?var x = mapPoint.x;// 102100系
?? ?var y = mapPoint.y;// 102100系
?? ?mapObj.graphics.clear();
?? ?var xqdR = $(“#xqdR”).val();//在页面上得到半径
?? ?addMarker_my(x, y);//添加点标记
?? ?require(
?? ??? ??? ?[ “esri/map”, “esri/layers/FeatureLayer”, “esri/tasks/query”,
?? ??? ??? ??? ??? ?“esri/geometry/Circle”, “esri/graphic”,
?? ??? ??? ??? ??? ?“esri/InfoTemplate”, “esri/symbols/SimpleMarkerSymbol”,
?? ??? ??? ??? ??? ?“esri/symbols/SimpleLineSymbol”,
?? ??? ??? ??? ??? ?“esri/symbols/SimpleFillSymbol”,
?? ??? ??? ??? ??? ?“esri/renderers/SimpleRenderer”, “esri/config”,
?? ??? ??? ??? ??? ?“esri/Color”, “dojo/dom”, “dojo/domReady!” ],
?? ??? ??? ?function(Map, FeatureLayer, Query, Circle, Graphic, InfoTemplate,
?? ??? ??? ??? ??? ?SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
?? ??? ??? ??? ??? ?SimpleRenderer, esriConfig, Color, dom) {
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?// esriConfig.defaults.io.proxyUrl = “/proxy/”;
?? ??? ??? ??? ??? ?esri.config.defaults.io.corsDetection = false;
?? ??? ??? ??? ??? ?var prourl = “”;
?? ??? ??? ??? ??? ?if (interestType == “餐饮”) {
?? ??? ??? ??? ??? ??? ?prourl = arcgisserverurl
?? ??? ??? ??? ??? ??? ??? ??? ?+ “ArcGIS/rest/services/HNBASE1129/MapServer/39″;
?? ??? ??? ??? ??? ?} else if (interestType == “酒店”) {
?? ??? ??? ??? ??? ??? ?prourl = arcgisserverurl
?? ??? ??? ??? ??? ??? ??? ??? ?+ “ArcGIS/rest/services/HNBASE1129/MapServer/130″;
?? ??? ??? ??? ??? ?} else {// (interestType == “景点”)
?? ??? ??? ??? ??? ??? ?prourl = arcgisserverurl
?? ??? ??? ??? ??? ??? ??? ??? ?+ “ArcGIS/rest/services/HNBASE1129/MapServer/116″;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?featureLayer = new FeatureLayer(prourl,
?? ??? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ??? ?infoTemplate : new InfoTemplate(
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?“Block: ${Name}”, “${*}”),
?? ??? ??? ??? ??? ??? ??? ??? ?outFields : [ “Name”, “Name_PY”, “Address”,
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?“Telephone” ]
?? ??? ??? ??? ??? ??? ??? ?});
?? ??? ??? ??? ??? ?var symbol = new SimpleMarkerSymbol(
?? ??? ??? ??? ??? ??? ??? ?SimpleMarkerSymbol.STYLE_CIRCLE, 12,
?? ??? ??? ??? ??? ??? ??? ?new SimpleLineSymbol(SimpleLineSymbol.STYLE_NULL,
?? ??? ??? ??? ??? ??? ??? ??? ??? ?new Color([ 247, 34, 101, 0.9 ]), 1),
?? ??? ??? ??? ??? ??? ??? ?new Color([ 207, 34, 171, 0.5 ]));
?? ??? ??? ??? ??? ?featureLayer.setSelectionSymbol(symbol);
?? ??? ??? ??? ??? ?// make unselected features invisible
?? ??? ??? ??? ??? ?var nullSymbol = new SimpleMarkerSymbol().setSize(0);
?? ??? ??? ??? ??? ?featureLayer.setRenderer(new SimpleRenderer(nullSymbol));
?? ??? ??? ??? ??? ?mapObj.addLayer(featureLayer);
?? ??? ??? ??? ??? ?var circleSymb = new SimpleFillSymbol(
?? ??? ??? ??? ??? ??? ??? ?SimpleFillSymbol.STYLE_NULL, new SimpleLineSymbol(
?? ??? ??? ??? ??? ??? ??? ??? ??? ?SimpleLineSymbol.STYLE_SHORTDASHDOTDOT,
?? ??? ??? ??? ??? ??? ??? ??? ??? ?new Color([ 105, 105, 105 ]), 2),
?? ??? ??? ??? ??? ??? ??? ?new Color([ 255, 255, 0, 0.25 ]));
?? ??? ??? ??? ??? ?circle = new Circle({
?? ??? ??? ??? ??? ??? ?center : mapPoint,
?? ??? ??? ??? ??? ??? ?geodesic : true,
?? ??? ??? ??? ??? ??? ?radius : xqdR
?? ??? ??? ??? ??? ?});
?? ??? ??? ??? ??? ?var graphic = new Graphic(circle, circleSymb);
?? ??? ??? ??? ??? ?mapObj.infoWindow.hide();
?? ??? ??? ??? ??? ?mapObj.graphics.add(graphic);
?? ??? ??? ??? ??? ?var query = new Query();
?? ??? ??? ??? ??? ?query.geometry = circle.getExtent();
?? ??? ??? ??? ??? ?featureLayer.queryFeatures(query, selectInBuffer);
?? ??? ??? ??? ?} catch (e) {
?? ??? ??? ??? ??? ?alert(“代理查询失败,请重新尝试”);
?? ??? ??? ??? ?}
?? ??? ??? ?});
}
function selectInBuffer(response) {
?? ?try{
?? ??? ?var feature;
?? ??? ?var features = response.features;
?? ??? ?var inBuffer = [];
?? ??? ?// filter out features that are not actually in buffer, since we got all
?? ??? ?// points in the buffer’s bounding box
?? ??? ?for ( var i = 0; i < features.length; i++) {
?? ??? ??? ?feature = features[i];
?? ??? ??? ?if (circle.contains(feature.geometry)) {
?? ??? ??? ??? ?inBuffer.push(feature.attributes[featureLayer.objectIdField]);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?var query = new esri.tasks.Query();
?? ??? ?query.objectIds = inBuffer;
?? ??? ?// use a fast objectIds selection query (should not need to go to the
?? ??? ?// server)
?? ??? ?featureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW,
?? ??? ??? ??? ?function(results) {
?? ??? ??? ??? ??? ?// 展现结果
?? ??? ??? ??? ??? ?showResults(results);
?? ??? ??? ??? ?});
?? ?}catch (e) {
?? ??? ?alert(“代理查询失败,请重新尝试”);
?? ?}
?? ?
}
function showResults(results) {
?? ?
?? ?var resultCount = results.length;
?? ?if (resultCount == 0) {
?? ??? ?$(“#xqdContent”).html(“查询异常”);
?? ??? ?$(“#xqdContent”).show();
?? ?}else{
?? ??? ?
?? ??? ?
?? ??? ?var resultStr = “<table>”;
?? ??? ?for ( var i = 0; i < resultCount; i++) {
?? ??? ??? ?var feature = results[i];
?? ??? ??? ?var x = feature.geometry.x;
?? ??? ??? ?var y = feature.geometry.y;
?? ??? ??? ?addMarker(x, y);
?? ??? ??? ?var name = feature.attributes.Name;
?? ??? ??? ?var phone = feature.attributes.Telephone;
?? ??? ??? ?var address = feature.attributes.Address;
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?resultStr += “<tr><td><h3><font color=\”#00a6ac\”>名称: “
?? ??? ??? ??? ?+ name + “</font></h3></td></tr>”;
?? ??? ??? ?resultStr += “<tr><td>” + phone + “<br/>” + address
?? ??? ??? ?+ “</td></tr>”;
?? ??? ??? ?
?? ??? ?}
?? ??? ?resultStr +=”</table>”;
?? ??? ?$(“#xqdContent”).html(resultStr);
?? ??? ?
?? ??? ?$(“#xqdContent”).show();
?? ?}
}
?
// 添加点标记
function addMarker_my(lngLatX, lngLatY) {
?? ?if (mapObj) {
?? ??? ?var symbol = new esri.symbol.PictureMarkerSymbol(appPath
?? ??? ??? ??? ?+ “/images/locationmy.png”, 20, 28);
?? ??? ?var pt = new esri.geometry.Point(lngLatX, lngLatY);
?? ??? ?
?? ??? ?var graphic = new esri.Graphic(pt, symbol);
?? ??? ?if (mapObj.graphics) {
?? ??? ??? ?mapObj.graphics.add(graphic);
?? ??? ?}
?? ??? ?
?? ?}
}
原文:http://rqlipeng.iteye.com/blog/2186890