将Google地图中心转到位置字符串

trs*_*trs 3 javascript google-maps geolocation google-maps-api-3

使用谷歌地图API,我想将地图居中一个字符串而不是Lat/Lng

var mapOptions = {
    center: "Paris"
};
Run Code Online (Sandbox Code Playgroud)

MrU*_*own 10

您可以使用地理编码器:

var geocoder;
var map;

function initialize() {

    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
        zoom: 8,
        center: latlng
    };

    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    // Call the codeAddress function (once) when the map is idle (ready)
    google.maps.event.addListenerOnce(map, 'idle', codeAddress);
}

function codeAddress() {

    // Define address to center map to
    var address = 'Paris, France';

    geocoder.geocode({
        'address': address
    }, function (results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            // Center map on location
            map.setCenter(results[0].geometry.location);

            // Add marker on 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);
        }
    });
}

initialize();
Run Code Online (Sandbox Code Playgroud)

JSFiddle demo

编辑:当然你可以像这样对一个完整的地址进行地理编码:

// Define address to center map to
var address = 'Rue Casse-Cul, Montboucher-sur-Jabron, France'; 
Run Code Online (Sandbox Code Playgroud)