Google地图区域突出显示并适当缩放" APPROXIMATE"区

时间:2015-01-10 06:46:42

标签: javascript php google-maps

有没有办法让它在使用google map api并搜索类型" APPROXIMATE"它将自动缩小并突出显示红色的大致区域 - 确切地说它将如何在Google上运行 - 使用谷歌地图API v3? http://i.stack.imgur.com/9vHS7.png

在我的网站上,当您搜索澳大利亚时,它会查询北领地,并将地图集中在一个随机点。我收到用户输入的地址并转换为lat / lng,然后将其发送到Google地图API。 (截图:http://i.stack.imgur.com/uQvAR.png)请告知是否还有其他方式,谢谢。

我像这样声明地图画布:

            map = new google.maps.Map(mapDiv, {
                center: new google.maps.LatLng(<?php echo $geo_latitude; ?>, <?php echo $geo_longitude; ?>),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                scrollwheel: false,
                 <?php echo(isMobile()) ? 'draggable: false' : ''; ?>
              });

这是解析用户将位置地址写入Lat / Long的功能。

// convert address to lat/long
function get_lat_long($address){

    $address = str_replace(" ", "+", $address);

    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
    $json = json_decode($json);

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    $types = $json->{'results'}[0]->{'types'};
    $location_type = $json->{'results'}[0]->{'geometry'}->{'location_type'};

    if ( $types[0] == 'administrative_area_level_1' &&  $types[1] == 'political') {
        $types = "STATE";
    } elseif ( $types[0] == 'country' && $types[1] == 'political' ) {
        $types = "COUNTRY";
    } else {
        $types = "OTHER";
    }
    return $lat.','.$long.','.$types.','.$location_type;
}

1 个答案:

答案 0 :(得分:0)

如果您使用Google Maps Javascript API v3 Geocoderit can contain a viewport and/or a bounds property in the result,则可以缩放地图,使其边界完全包含结果。

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