Bu konuyu tartıştığımı biliyorum ancak kodum hala çalışmıyor. google places searchBox API kullanıyorum.arama kutucusundan enlem ve boylamı alın
Kodum:
function initMap(){
var map = new google.maps.Map(document.getElementById('peta'), {
center: {lat: -34.397, lng: 150.644},
zoom: 16
});
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Your Location.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
var input = document.getElementById('alamat');
var searchBox = new google.maps.places.SearchBox(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// 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();
places.forEach(function(place) {
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
document.getElementById('x').innerHTML(places.geometry.location.lat());
});
}
bağlantı komut:
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&libraries=places"></script>
html:
benim metin üzerindedocument.getElementById
koymak ama hala enlem göstermiyor
<div id="peta" style="width:500px;height:380px;"></div>
<input type="text" id="x" readonly="readonly" name="x">
. Bu kodu nereye koymalıyım?
diğer thread:
var lat = location.lat();
fakat bu da işe yaramıyor. Bu soruyu tekrar edersem özür dilerim. teşekkür ederim.
Gönderilen kodla ilgili bir javascript hatası alıyorum: 'Uncaught ReferenceError: map tanımlı değil '. Lütfen sorununuzu gösteren [Minimal, Tam, Test Edilebilir ve Okunabilir bir örnek] (http://stackoverflow.com/help/mcve) belirtin. Ve 'Uncaught TypeError: Bu satırın null değerinin 'value' değerini okuyamazsınız '' input = document.getElementById ('alamat');' (HTML'de bulunmayan). – geocodezip
tamam. üzgünüm. açıklamayı güncelledim. – daniel