2012-09-27 28 views
22

Xin chào, tôi đang sử dụng mã này để sử dụng các chỉ đường trong bản đồ google. nhưng khi tôi chạy nó từ các máy khác, nó sẽ hiển thị khóa api lỗi không hợp lệ. cách sử dụng cùng một bản đồ này mà không có khóa api.Làm thế nào để sử dụng bản đồ google mà không cần phím api

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script> 
    <style type="text/css"> 
    body { 
     background:#FFE1C7; 
     font-family: Verdana, Arial, sans serif; 
     font-size: 11px; 
     margin: 2px; 
    } 
table.directions th { 
     background-color:#EEEEEE; 
    } 

    img { 
     color: #000000; 
    } 
    </style> 
    <script type="text/javascript"> 

    var map; 
    var gdir; 
    var geocoder = null; 
    var addressMarker; 

    function initialize() { 
     if (GBrowserIsCompatible()) {  
     map = new GMap2(document.getElementById("map_canvas")); 
     gdir = new GDirections(map, document.getElementById("directions")); 
     GEvent.addListener(gdir, "load", onGDirectionsLoad); 
     GEvent.addListener(gdir, "error", handleErrors); 

     setDirections("Bangalore", "Mysore", "en_US"); 
     } 
    } 

    function setDirections(fromAddress, toAddress, locale) { 
     gdir.load("from: " + fromAddress + " to: " + toAddress, 
       { "locale": locale }); 
    } 

    function handleErrors(){ 
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) 
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code); 
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) 
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code); 

    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) 
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code); 

    // else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong 
    //  alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code); 

    else if (gdir.getStatus().code == G_GEO_BAD_KEY) 
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code); 

    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) 
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code); 

    else alert("An unknown error occurred."); 
    } 

    function onGDirectionsLoad(){ 
     // Use this function to access information about the latest load() 
     // results. 

     // e.g. 
     // document.getElementById("getStatus").innerHTML = gdir.getStatus().code; 
     // and yada yada yada... 
    } 

    </script> 

bất cứ ai có thể giúp tôi ... cảm ơn trước ..

+0

thấy điều này ... http: //stackoverflow.com/questions/2187010/using-google-maps-without-api-keys – nKandel

Trả lời

26

bạn cần thay đổi API của bạn từ V2 để V3, Kể từ khi Google Map Phiên bản 3 không cần API chính

Kiểm tra this ra ..

viết của bạn script như

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

EDIT: Câu trả lời này hiện đã lỗi thời. Bây giờ bạn được yêu cầu có một khóa API để sử dụng bản đồ google. Xem bình luận dưới đây.

+0

Tôi cũng đã thử bằng cách thay đổi thành v3 nhưng khi tôi thay đổi thành v3 bản đồ sẽ biến mất . –

+1

yo phải thay đổi mã của bạn một chút ... vì cú pháp gọi Map là thay đổi trong Google Map v3 ... http: //www.birdtheme.org/useful/v3tool.html sẽ giúp bạn thay đổi mã theo V3 .. – Usman

+0

Trên thực tế cảm biến là không cần thiết. – candlejack

16

Bây giờ bạn phải có khóa API. Bạn có thể tạo điều đó trong bảng điều khiển dành cho nhà phát triển của Google. Dưới đây là LINK để giải thích.

+1

Để biết thông tin, tôi đã tìm thấy giải pháp thay thế để tiếp tục sử dụng API Google Maps mà không có khóa: http://stackoverflow.com/questions/38148097/google-maps- api-without-key/38809129 # 38809129 – baptx

Các vấn đề liên quan