I've looked all over this site but couldnt find a clear cut answer. First of all i'm a javascript noob.
I'm trying to setup a google maps where it gets it input from a textbox and then outputs the Lat/Long in a two different textboxes. .
There are 3 things I want to do.
- Use a textbox(named izipcode) as input for geocoding when I click on a button that says "Geocode.
- When i drag the marker to a new location, the textboxes have to be updated with the new Lat/Long. As of right now I can get the lat/long in a the right boxes but cant get them to update once i move the marker.
- I want to use only one marker, now when i click it just keeps making new markers. By the way my company is using Apache Wicket and thats why i have a seperate JS File.
This is my HTML.
<wicket:extend>
<div class="message_err" wicket:id="feedback">[feedback panel]</div>
<div class="title" wicket:id="merchantstore.title"/>
<form style="margin-top: 1em;" wicket:id="form">
<fieldset>
<table>
<thead></thead>
<tbody>
<tr>
<td><label for="iname"><wicket:message key="label.merchantstore.address">Address</wicket:message> <em>*</em></label></td>
<td><input id="iname" type="text" class="text" wicket:id="merchantstore.address" /></td>
</tr>
<tr>
<td><label for="icountry"><wicket:message key="label.merchantstore.countrycode">Country</wicket:message> <em>*</em></label></td>
<td><input id="icountry" type="text" class="text" wicket:id="merchantstore.countrycode" /></td>
</tr>
<tr>
<td><label for="icity"><wicket:message key="label.merchantstore.city">City</wicket:message> <em>*</em></label></td>
<td><input id="icity" type="text" class="text" wicket:id="merchantstore.city" /></td>
</tr>
<
<tr>
<td><label for="izipcode"><wicket:message key="label.merchantstore.zipcode">Zip code</wicket:message> <em>*</em></label></td>
<td><input id="izipcode" type="text" class="text" wicket:id="merchantstore.zipcode" value="" /></td>
<td><input type="button" class="geocode" onclick="codeAddress()" wicket:message="value:button.geocode" /></td></tr>
<tr>
<td><label for="ilatitude"><wicket:message key="label.merchantstore.latitude">Latitude</wicket:message></label></td>
<td><input id="ilatitude" type="text" class="text" wicket:id="merchantstore.latitude" /></td>
</tr>
<tr>
<td><label for="ilongitude"><wicket:message key="label.merchantstore.longitude">Longitude</wicket:message></label></td>
<td><input id="ilongitude" type="text" class="text" wicket:id="merchantstore.longitude" /></td>
</tr>
<td><div id="gmap" style="width: 512px; height:512px; margin-left: auto; margin-right: auto; margin-top: 50px;"></div></td>
</tbody>
</table>
</fieldset>
<p>
<input type="submit" class="submit fixedbutton" wicket:message="value:button.save"/>
<input type="button" class="fixedbutton" wicket:message="value:button.cancel" wicket:id="actionCancel" />
</p>
</form>
</wicket:extend>
This is my JS file.
function contactMap() {
var myLatlng = new google.maps.LatLng(0,0);
var mapOptions = {
zoom: 1,
minZoom: 1,
center: myLatlng,
disableDefaultUI: false,
navigationControl: false,
mapTypeControl: false,
scrollwheel: true,
// styles: styles,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var geocoder
geocoder = new google.maps.Geocoder();
function codeAddress(){
var Address=document.getElementById("izipcode").value;
geocoder.geocode({ 'izipcode': address }, function(results, status){if (status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
draggable:true,
position: results[0].geometry.location });
}
else
{
alert("Geocode is niet gelukt" + status);
};
})
}
var map = new google.maps.Map(document.getElementById('gmap'), mapOptions);
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
google.maps.event.addListener(map, "click", function(event) {
// get lat/lon of click
var clickLat = event.latLng.lat();
var clickLon = event.latLng.lng();
// show in input box
document.getElementById("ilatitude").value = clickLat.toFixed(15);
document.getElementById("ilongitude").value = clickLon.toFixed(15);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(clickLat,clickLon),
map: map,
draggable: true,
});
});
// keep googlemap responsive - center on resize
google.maps.event.addDomListener(window, 'resize', function() {
map.setCenter(myLatlng);
});
};
function showMap(initWhat) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&callback='+initWhat;
document.body.appendChild(script);
}
$( document ).ready(function() {
// INIT CONTACT, NLY IF #contactMap EXISRS
if(jQuery("#gmap").length > 0) {
showMap('contactMap');
}
});