यह लगता है बेकार में जटिल यहाँ एक उदाहरण है "आस-पास की घटनाओं" नक्शा यह City, State
रों ले जाएगा, उन्हें latLng
coords में बदलने का है, और एक पर मार्कर डाल दिया।। नक्शा:।
// 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
अपने <script>
टैग शामिल करना सुनिश्चित करें
<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 बाकी और जी हर किसी को शामिल होने के लिए कृपया सिंटैक्स हाइलाइटिंग के साथ et GitHub मार्कडाउन ...
क्या PHP के साथ ऐसा करने का कोई तरीका है? धन्यवाद – lisovaccaro