如果我要搜索文本地址(假设地址是“305 Quincy St NE”),我将如何使用ArcGIS API for Javascript在ArcGIS中执行此操作?
我的目标是调用一个函数,该函数将一个地方的几何作为其函数参数传递。
function showLocation(evt) {
map.graphics.clear();
var point = evt.result.feature.geometry; // how do I get this value from a textual address?
.
.
.
}
答案 0 :(得分:2)
使用Locator
课程。这是一个例子:
<!DOCTYPE html>
<html>
<head>
<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>Using Locator to Find Address</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.14/esri/css/esri.css">
<style>
html, body, #map {
height:100%;
width:100%;
margin:0;
padding:0;
}
body {
background-color:#FFF;
overflow:hidden;
font-family:"Trebuchet MS";
}
#search {
display: block;
position: absolute;
z-index: 2;
top: 20px;
left: 75px;
}
</style>
<script src="https://js.arcgis.com/3.14/"></script>
<script>
var map;
var locator;
var symbol;
var locatorUrl = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer";
require([
"esri/map", "esri/tasks/locator", "esri/graphic",
"esri/Color", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol",
"dijit/form/TextBox", "dijit/registry", "dojo/on", "dojo/parser",
"dojo/keys",
"dojo/domReady!"
], function(
Map, Locator, Graphic,
Color, SimpleMarkerSymbol, SimpleLineSymbol,
TextBox, registry, on, parser,
keys
) {
map = new Map("map",{
basemap: "topo",
center: [-117.19, 34.05],
zoom: 13
});
locator = new Locator(locatorUrl);
symbol = new SimpleMarkerSymbol(
SimpleMarkerSymbol.STYLE_CIRCLE,
20,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
Color.fromHex("#EEAA00"),
3),
Color.fromHex("#002255")
);
parser.parse();
var box = registry.byId("textBox_address");
on(box, "keypress", function (evt) {
if (keys.ENTER == evt.keyCode) {
var addressText = box.get("value");
//This is the important part. Call Locator.addressToLocations.
locator.addressToLocations({
address: {
SingleLine: addressText
}
}, function (addresses) {
//Success
if (0 == addresses.length) {
alert("Address not found");
} else {
var address = addresses[0];
var pt = address.location;
map.graphics.clear();
map.graphics.add(new Graphic(pt, symbol));
map.centerAt(pt);
}
}, function (error) {
//Failure
alert("Error: " + error);
});
}
});
});
</script>
</head>
<body>
<div id="search">
<label for="firstname" style="background-color: white">Address:</label>
<input type="text" name="textBox_address" value="808 Travis St, Houston TX"
data-dojo-type="dijit/form/TextBox" id="textBox_address" />
</div>
<div id="map"></div>
</body>
</html>
答案 1 :(得分:1)
var address = "305 Quincy St NE";
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var longitude= results[0].geometry.location.lat();
var latitude = results[0].geometry.location.lng();
}
});
答案 2 :(得分:0)
关于Hiren的回答,
问题不在于纬度和经度,问题是您不能将谷歌地理编码器与谷歌地图不同的任何其他技术使用:
地理编码API可能只能与Google地图结合使用; 禁止在地图上显示地理编码结果。对于 有关允许使用的完整详细信息,请参阅Maps API条款 服务许可限制。
你可以在这里查看: https://developers.google.com/maps/documentation/geocoding/intro
干杯!