Google maps 如何通过googlejavascript-api获取位置坐标
我知道地点id,我需要知道它的坐标 我可以做Google maps 如何通过googlejavascript-api获取位置坐标,google-maps,google-maps-api-3,google-geocoder,Google Maps,Google Maps Api 3,Google Geocoder,我知道地点id,我需要知道它的坐标 我可以做GEThttps://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE 这给了我类似的东西: ``` "results" : [ { "address_components" : [...], "formatted_address" : "New York C
GEThttps://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE
这给了我类似的东西:
``` "results" : [
{
"address_components" : [...],
"formatted_address" : "New York County, NY, USA",
"geometry" : {
"bounds" : {...},
"location" : {
"lat" : 40.7830603,
"lng" : -73.9712488
},
"location_type" : "APPROXIMATE",
"viewport" : {...}
},
"partial_match" : true,
"place_id" : "ChIJOwE7_GTtwokRFq0uOwLSE9g",
"types" : [...]
}
],
"status" : "OK"
```
但我需要通过javascript api来实现
我的页面上没有任何地图,只需要获取坐标。您可以像这样检索它的位置
var obj = JSON.parse(yourJSONstring);
obj.results[0].geometry.location.lat;
obj.results[0].geometry.location.lng;
发件人(带您的地址号):
代码段:
var地理编码器;
var映射;
函数初始化(){
var map=new google.maps.map(
document.getElementById(“地图画布”){
中心:新google.maps.LatLng(37.4419,-122.1419),
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var请求={
placeId:'chijowe7gttwokrfq0uowlse9g'
};
var infowindow=new google.maps.infowindow();
var service=newgoogle.maps.places.PlacesService(地图);
service.getDetails(请求、功能(位置、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
var marker=new google.maps.marker({
地图:地图,
位置:place.geometry.location
});
google.maps.event.addListener(标记'click',函数(){
infowindow.setContent(place.name);
打开(地图,这个);
});
map.fitBounds(place.geometry.viewport);
}
});
}
google.maps.event.addDomListener(窗口“加载”,初始化)代码>
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}
发生这种情况是因为location属性的类型为
要获得lat和lng,可使用以下功能:
例如:
您可以在这里阅读更多内容:。您不需要地图。它是一个返回JSON字符串的Restful web服务。在应用程序中,您只需要向发送HTTP请求
得到
然后解析返回的JSON字符串,并检索其中的latlng
如果您在web应用程序中使用Jquery,您可以这样做
$.ajax({
type: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE",
dataType: "text",
success: function (yourJSONstring){
var respJson = $.parseJSON(yourJSONstring);
// then something like ...
// respJson.results[0].geometry.location.lat;
// respJson.results[0].geometry.location.lng;
},
error: function (xhr) {
}
});
})) 如果您使用此代码段[删除源输入和目标输入下的设置字段。使用上面已经给出的函数autocomplete.addListener,但添加autocomplete.setFields(['place_id','geometry','name','formatted_address']);var lng=place.geometry.location.lng();var-lat=place.geometry.location.lat();var-latlng={lat,lng};console.log(latlng);我看不出这个答案被投票的原因。OP说,但我需要通过javascript api来实现。
但这个答案没有显示这一点。
place.geometry.location.any_function_below()
1. toString(): Converts to string representation.eg ((37.8042802, -122.41364429999999))
2. lat(): Returns the latitude in degrees.
3. lng(): Returns the longitude in degrees.
4. toJSON(): Converts to JSON representation ({lat: "", lng: ""}).
5. toUrlValue(precision?:number): Returns a string of the form "lat,lng" for this LatLng. We round the lat/lng values to 6 decimal places by default.
$.ajax({
type: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE",
dataType: "text",
success: function (yourJSONstring){
var respJson = $.parseJSON(yourJSONstring);
// then something like ...
// respJson.results[0].geometry.location.lat;
// respJson.results[0].geometry.location.lng;
},
error: function (xhr) {
}
});
`//only add these line under function`
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
autocomplete.setFields(['place_id', 'geometry', 'name', 'formatted_address']);
var lng = place.geometry.location.lng();
var lat = place.geometry.location.lat();
var latlng = {lat , lng};
console.log(latlng);