如何通过选择城市从谷歌地图获取地点?我的代码如下

时间:2018-06-17 07:08:25

标签: javascript php

geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeControl: true,
    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
    },
    navigationControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-container"),
    myOptions);
if (geocoder) {
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                map.setCenter(results[0].geometry.location);
                mlat = results[0].geometry.location.lat();
                mlng = results[0].geometry.location.lng();
                var infowindow = new google.maps.InfoWindow({
                    content: '<b>You are here</b>',
                    size: new google.maps.Size(150, 50)
                });
                var marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map,
                    title: "You are here"
                });

                addInfoWindow(marker, "You are here");
                checkRange(mlat, mlng);
            } else {
                alert("No results found");
            }
        } else {
            alert("Geocode was not successful for the following reason: " +
                status);
        }
    });

这是我的谷歌地图代码。我正在使用地理编码器请告诉我如何才能获得城市和地区。我能够获得拉特和土地,但我试图找到城市和地区。

0 个答案:

没有答案