2016-01-12 29 views

回答

1

我使用的onload功能該功能initMap工作對我來說我希望這將有助於您

function initMap() { 
      map = new google.maps.Map(document.getElementById('mapSection'), { 
       center: { lat: 13.0480787, lng: 79.9288088 }, 
       zoom: 10, 
       mapTypeId: google.maps.MapTypeId.ROADMAP, 
       zoomControl: true, 
       zoomControlOptions: { 
        position: google.maps.ControlPosition.LEFT_CENTER 
       }, 

      }); 

     // Create the search box and link it to the UI element. 
    var input = document.getElementById('pac-input'); 
    var searchBox = new google.maps.places.SearchBox(input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

    // Bias the SearchBox results towards current map's viewport. 
    map.addListener('bounds_changed', function() { 
    searchBox.setBounds(map.getBounds()); 
    var bounds = map.getBounds(); 
    }); 

    var markers = []; 
    var geocoder = new google.maps.Geocoder(); 

    searchBox.addListener('places_changed', function() { 
    var places = searchBox.getPlaces(); 

    if (places.length == 0) { 
     return; 
    } 
    geocodeAddress(geocoder, map);  
    // Clear out the old markers. 
     markers.forEach(function(marker) { 
     marker.setMap(null); 
    }); 
    markers = []; 
    // For each place, get the icon, name and location. 
    var bounds = new google.maps.LatLngBounds(); 
    map.fitBounds(bounds); 
    }); 
    // [END region_getplaces] 


    function geocodeAddress(geocoder, resultsMap) { 

       var address = document.getElementById('pac-input').value; 
       geocoder.geocode({'address': address}, function(results, status) { 
       if (status === google.maps.GeocoderStatus.OK) { 

       } else { 
        alert('Geocode was not successful for the following reason: ' + status); 
       } 
       }); 
      } 
} 

,如果你想在地圖上的標記使用此功能

function create_Marker(lat, lan, drivername, indx) { 

    var MARKER = new google.maps.Marker({ 
     position : new google.maps.LatLng(lat, lan), 
     map : MAP, 
     icon : "images/car.png", 
     title : drivername, 
     animation : google.maps.Animation.DROP 
    }); 
    MARKER_List.push(MARKER); 
    google.maps.event.addListener(MARKER, 'click', (function(marker, indx) { 
     return function() { 
      infowindow.setContent("<div class='content' style='max-height:300px; font-size:12px;';'>" + drivername 
        + "</div>"); 
      infowindow.open(MAP, marker); 
     } 
    })(MARKER, indx)); 
} 
+0

不過,這對我沒有幫助 – quak