-
Notifications
You must be signed in to change notification settings - Fork 4
/
Get Latt-Long On Click Event
76 lines (65 loc) · 2.71 KB
/
Get Latt-Long On Click Event
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
This demonstrate how to get Lattitude and Longitude based on user click event.
I. Inside tag <head></head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<script>
function initialize() {
var markers = [];
var map = new google.maps.Map(document.getElementById('map-canvas'), {
mapTypeId: google.maps.MapTypeId.SATELLITE // I pick satellite for this. you can change into e.g. ROADMAP.
});
google.maps.event.addListener(map,'click',function(event) {
document.getElementById('txtlattlong').value = event.latLng.lat() + ', ' + event.latLng.lng()
})
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));
map.fitBounds(defaultBounds);
var input = /** @type {HTMLInputElement} */(
document.getElementById('qfront'));
//map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
markers = [];
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(21, 21),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(18, 18)
};
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});
markers.push(marker);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
});
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
II. Inside Body tag:
a. Input Search:
<input id="qfront" name="qfront" placeholder="type a location here ..." type="text"/>
b. Maps show here:
<div id="map-canvas"></div>
c. Result here:
<input type="text" id="txtlattlong" name="txtlattlong" class="" placeholder="result here ..." value=""/>
Note:Also available at http:https://www.wwwsitus.com