Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从搜索框中获取纬度和经度_Javascript_Php_Google Maps - Fatal编程技术网

Javascript 从搜索框中获取纬度和经度

Javascript 从搜索框中获取纬度和经度,javascript,php,google-maps,Javascript,Php,Google Maps,我知道很多线程都在讨论这个问题,但我的代码仍然不起作用 我使用的是google places searchBox API 我的代码: 函数initMap(){ var map=new google.maps.map(document.getElementById('peta'){ 中心:{纬度:-34.397,液化天然气:150.644}, 缩放:16 }); var infoWindow=new google.maps.infoWindow({map:map}); //试试HTML5地理定位。

我知道很多线程都在讨论这个问题,但我的代码仍然不起作用

我使用的是google places searchBox API

我的代码:

函数initMap(){
var map=new google.maps.map(document.getElementById('peta'){
中心:{纬度:-34.397,液化天然气:150.644},
缩放:16
});
var infoWindow=new google.maps.infoWindow({map:map});
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
var pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
信息窗口。设置位置(pos);
infoWindow.setContent(“您的位置”);
地图设置中心(pos);
},函数(){
handleLocationError(true,infoWindow,map.getCenter());
});
}否则{
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
}
var input=document.getElementById('alamat');
var searchBox=newgoogle.maps.places.searchBox(输入);
//将搜索框结果偏向当前地图的视口。
addListener('bounds_changed',function(){
searchBox.setBounds(map.getBounds());
});
var标记=[];
//侦听用户选择预测并检索时激发的事件
//关于那个地方的更多细节。
searchBox.addListener('places\u changed',function(){
var places=searchBox.getPlaces();
如果(places.length==0){
回来
}
//清除旧的标记。
markers.forEach(函数(marker){
marker.setMap(空);
});
标记=[];
//对于每个位置,获取图标、名称和位置。
var bounds=new google.maps.LatLngBounds();
地点。forEach(功能(地点){
变量图标={
url:place.icon,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
};
//为每个地方创建一个标记。
markers.push(新的google.maps.Marker)({
地图:地图,
图标:图标,
标题:place.name,
位置:place.geometry.location
}));
if(place.geometry.viewport){
//只有地理代码具有视口。
联合(place.geometry.viewport);
}否则{
扩展(place.geometry.location);
}
});
映射边界(bounds);
document.getElementById('x').innerHTML(places.geometry.location.lat());
});
}
链接脚本:


html:


我将
document.getElementById
放在文本框中,但它仍然没有显示纬度。我应该把代码放在哪里
在其他线程中:

var location=places.geometry.location;
var lat=location.lat();
但它不起作用。如何解决此问题?

您的元素#x是一个输入文本框吗

如果是,请尝试:

 document.getElementById('x').value = places.geometry.location.lat();
这将为输入文本框的值添加纬度


这就是你想要达到的目标吗?

我已经解决了这个问题

正如@Dammeul所说,我将

document.getElementById('x')。value=place.geometry.location.lat()

places.forEach(函数(位置))的内部

希望这有帮助。

它会将地址转换为纬度和经度,并用标记在地图中显示地址。您只需添加地图API键

var地理编码器;
var映射;
函数initMap(){
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(23.684994,90.35633099995);
变量映射选项={
缩放:8,
中心:拉丁
}
map=new google.maps.map(document.getElementById('map'),mapOptions);
}
函数代码地址(){
var address=document.getElementById('address')。值;
geocoder.geocode({'address':address},函数(结果,状态){
如果(状态=‘正常’){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:结果[0]。几何体。位置
});
}否则{
警报('地理编码因以下原因未成功:'+状态);
}
});
}
函数showResult(结果){
document.getElementById('latitude')。value=result.geometry.location.lat();
document.getElementById('longitude')。value=result.geometry.location.lng();
}
函数getLatitudeLongitude(回调,地址){
地址=地址| |‘孟加拉国达卡’;
var geocoder=new google.maps.geocoder();
if(地理编码器){
地理编码({
“地址”:地址
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
回调(结果[0]);
}
});
}
}
var button=document.getElementById('show');
按钮。addEventListener(“单击”),函数(){
var address=document.getElementById('address')。值;
GetLatitudeLongTude(显示结果、地址)
});

/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
高度:400px;
宽度:800px;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}
纬度:

经度:

const{GoogleMap,LoadScript}=require(“../”);
const ScriptLoaded=require(“../../docs/ScriptLoaded”)。默认值;
欺骗
const { GoogleMap, LoadScript } = require("../../");
const ScriptLoaded = require("../../docs/ScriptLoaded").default;

const mapContainerStyle = {
  height: "400px",
  width: "800px"
};

const center = {
  lat: 38.685,
  lng: -115.234
};

const onLoad = ref => this.searchBox = ref;

const onPlacesChanged = () => {
    
      x = this.searchBox.getPlaces()
      console.log(x[0]["geometry"]["location"].lat())
    
    
    }
    

<ScriptLoaded>
  <GoogleMap
    id="searchbox-example"
    mapContainerStyle={mapContainerStyle}
    zoom={2.5}
    center={center}
  >
    <StandaloneSearchBox
      onLoad={onLoad}
      onPlacesChanged={
        onPlacesChanged
      }
    >
      <input
        type="text"
        placeholder="Customized your placeholder"
        style={{
          boxSizing: `border-box`,
          border: `1px solid transparent`,
          width: `240px`,
          height: `32px`,
          padding: `0 12px`,
          borderRadius: `3px`,
          boxShadow: `0 2px 6px rgba(0, 0, 0, 0.3)`,
          fontSize: `14px`,
          outline: `none`,
          textOverflow: `ellipses`,
          position: "absolute",
          left: "50%",
          marginLeft: "-120px"
        }}
      />
    </StandaloneSearchBox>
  </GoogleMap>
</ScriptLoaded>