Javascript 尝试使用google api v3从地址获取位置

Javascript 尝试使用google api v3从地址获取位置,javascript,google-maps,google-maps-api-3,geocoding,Javascript,Google Maps,Google Maps Api 3,Geocoding,我的html中有一个javascript,我想在这里得到一个给定位置的地图表示 <script type="text/javascript"> var geocoder; var map; function getmaploc() { geocoder = new google.maps.Geocoder(); var myOptions = { zoom: 8, mapTypeId: google.maps.MapTypeId.ROA

我的html中有一个javascript,我想在这里得到一个给定位置的地图表示

<script type="text/javascript">
var geocoder;
var map;
function getmaploc() {
    geocoder = new google.maps.Geocoder();
    var myOptions = {
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    geocoder.geocode( 'cairo', function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    });
}
</script>

<body onload="getmaploc()">
  <div id="map_canvas"  style="width: 320px; height: 480px;"></div>
</body> 

var地理编码器;
var映射;
函数getmaploc(){
geocoder=新的google.maps.geocoder();
变量myOptions={
缩放:8,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
geocoder.geocode('cairo',函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:结果[0]。几何体。位置
});
}否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
});
}

我想不出这有什么问题有什么帮助吗???

有多个错误

  • 省略的大括号
  • 首先初始化映射
  • geocode()需要一个GeocoderRequest对象作为参数

固定代码:

var geocoder;
var map;
function getmaploc() {
    geocoder = new google.maps.Geocoder();
    var myOptions = {
        zoom : 8,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    geocoder.geocode({
        address : 'cairo'
    }, function(results, status) {
        console.log(results);
        if(status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            new google.maps.Marker({
                map : map,
                position : results[0].geometry.location
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }

    });
}

有多个错误

  • 省略的大括号
  • 首先初始化映射
  • geocode()需要一个GeocoderRequest对象作为参数

固定代码:

var geocoder;
var map;
function getmaploc() {
    geocoder = new google.maps.Geocoder();
    var myOptions = {
        zoom : 8,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    geocoder.geocode({
        address : 'cairo'
    }, function(results, status) {
        console.log(results);
        if(status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            new google.maps.Marker({
                map : map,
                position : results[0].geometry.location
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }

    });
}

请格式化你的代码,很多人(包括我)不会阅读格式不好的代码修复了它我很抱歉我是新手StackOverflow为你重新格式化了它,你的修复仍然不是很可读。这里还有一个提示:“它不工作”如果没有错误消息,会让其他人更难提供帮助。多亏了,代码现在看起来更干净了:D问题已解决请格式化代码,许多人(包括我)不会阅读格式不好的代码已修复我很抱歉我是新来的StackOverflow为您重新格式化它,您的修复仍然不太可读。这里还有另一个提示:“它不工作”如果没有错误信息,会让其他人更难帮助。非常感谢代码现在看起来更干净:D问题解决了请不要抱怨我的缩进,我喜欢它^非常感谢已经解决了问题Molle博士请不要抱怨我的缩进,我喜欢它^非常感谢已经解决了问题Molle博士