【问题标题】:Can you make a map based off address firebase? [duplicate]你能制作一个基于地址外火力基地的地图吗? [复制]
【发布时间】:2022-01-18 00:53:42
【问题描述】:

我正在尝试制作一个程序,根据用户输入的地址创建一个谷歌地图。由于我无法为每个页面手动嵌入链接,我该如何制作,以便当用户使用地址回答表单时,我可以获取该地址并制作谷歌地图。我是否必须将地址转换为纬度和经度,然后再这样做?

【问题讨论】:

标签: javascript html firebase google-maps-api-3


【解决方案1】:

关于 Google maps javascript API,您需要纬度 (lat) 和经度 (lng) 才能在地图上放置标记。要查找地址的纬度和经度,您需要地理编码服务。

地理编码是将地址(例如“1600 Amphitheatre Parkway, Mountain View, CA”)转换为地理坐标(例如纬度 37.423021 和经度 -122.083739)的过程,您可以使用它在地图上放置标记或定位地图。

它存在很多地理编码服务,谷歌云平台提供了一种。首先,您需要在您的帐户中激活 Geocoding API(请参阅文档中的 this page)。

通过使用geocoding API

要查找地址(例如:“1600 Amphitheatre Parkway, Mountain View, CA 94043, USA”),只需简单的获取 API 请求,例如

https://maps.googleapis.com/maps/api/geocode/jsonaddress=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

您可以从 JSON 响应中获取 lat/lng,并使用它在地图上放置您的标记。为此,只需按照the documentation 的说明进行操作,您就可以通过执行以下操作来放置标记

let myLatLng = {lat:myLatitude, lng:myLongitude};
new google.maps.Marker({
    position: myLatLng,
    map
});

通过使用Geocoder()

就像@JeremyW 说的那样,您也可以使用 Google 地图 JavaScript API 中的 Geocoder 类调用地理编码服务(请参阅 this post)。

let geocoder = new google.maps.Geocoder();

那么……

let adress = "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA";
geocoder.geocode( { 'address': address}, function(results, status){
    if(status == google.maps.GeocoderStatus.OK){
        if(status != google.maps.GeocoderStatus.ZERO_RESULTS){      
            let marker = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map
            });
         }
         else{
             console.log("No results found");
         }
    }
    else{
        console.log("Geocode was not successful for the following reason: " + status);
    }
}

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多