2012-06-22 17 views

cevap

45

Kodun burada jsfiddled şekilde bulabilirler: Aşağıda http://jsfiddle.net/YphZw/ veya:

$("#btn").click(function(){ 
 
      var geocoder = new google.maps.Geocoder(); 
 
    geocoder.geocode({ 'address': 'miami, us'}, function(results, status) { 
 
      if (status == google.maps.GeocoderStatus.OK) { 
 
      alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
 
      } else { 
 
      alert("Something got wrong " + status); 
 
      } 
 
     }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
<head> 
 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
 
</head> 
 
<body> 
 
    <input id="btn" type="button" value="search for miami coordinates" /> 
 
</body> 
 
</html>

Eğer daha fazla örnek isterseniz Javascript API'sı için şu bağlantıyı deneyin: https://developers.google.com/maps/documentation/javascript/examples/

Yazdığım kod, coğrafi kodlama basit örneğinden esinlenmiştir.

Saygılarımızla.

DÜZENLEME 1:

Bir resmi olmayan bir PHP kütüphanesi kullanarak elde edebilirsiniz. Bu örneği kontrol edin: http://www.bradwedell.com/phpgooglemapapi/demos/geocoding.php (kod = alt kısmında

+0

PHP ile aynı şeyi yapmanın bir yolu var mı? Teşekkürler – lisovaccaro

12

Sen mesela Google'ın coğrafi kodlama hizmeti, biçimlerde (JSON, XML, vb) çeşitli size geri coğrafi başvuru verileri verir

http://maps.googleapis.com/maps/api/geocode/xml?address=Miami+FL&sensor=false

kullanabilirsiniz. Her halükarda, yer kesinlikle iade edilen veri bloğundadır. En

API docs

gibidir:

https://developers.google.com/maps/documentation/geocoding/

+0

Yukarıda belirttiğiniz API Dokümanlarının bağlantısı ve anlattığınız yöntem, Javascript'in değil, Google Haritalar API Web Hizmetleri ile ilgilidir. – Zakaria

+0

Doh! Maalesef - JS API'sına (çok simllar) giden link bu sayfadadır. – debracey

2

Bu görünüyor gereksiz karmaşık İşte bir örnek "yakındaki olaylar" Harita Bu City, State s almak latLng coords onları dönüştürmek ve bir üzerindeki işaretçileri koyacağız.. harita:.

// Nearby Events with Google Maps 
window.nearbyEventsMap =() => { 
    const centerOfUS = { 
     lat: 37.09024, 
     lng: -95.712891 
    } 

    // Create a map object and specify the DOM element for display. 
    const map = new google.maps.Map(document.querySelector('#nearby_events_map'), { 
     center: centerOfUS, 
     scrollwheel: false, 
     zoom: 4 
    }) 

    // Create a marker and set its position. 
    const geocoder = new google.maps.Geocoder() 

    // Filter out duplicate cityStates 
    let cityStates = {} 
    document.querySelectorAll('.nearby_event_city_state').forEach(event => { 
     cityStates[event] = event.innerText 
    }) 

    // `cityState` is in the format of "City, State". It's not picky about state being a word or the abbreviation. 
    for (const cityState in cityStates) { 
     const location = cityStates[cityState] 

     geocoder.geocode({ 
      address: location 
     }, function (results, status) { 
      if (status === 'OK') { 
       const result = results[0].geometry.location 
       const lat = result.lat() 
       const lng = result.lng() 
       const latLng = { 
        lat, 
        lng 
       } 

       return new google.maps.Marker({ 
        map: map, 
        position: latLng 
       }) 
      } 
     }) 
    } 
} 
// /Nearby Events with Google Maps 

sizin <script> etiketlerini dahil etmek emin olun

<script src="/dist/js/main.js"></script> 

<!-- We're loading this after the main.js script so the callback from main.js will be available to it. --> 
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=nearbyEventsMap"></script> 

StackOverflow başka ve g herkesi katılın sözdizimi vurgulama ile GitHub işaretleme ...

İlgili konular