Huidige locatie data in een textbox weergeven

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Sebas V

Sebas V

24/01/2012 00:41:31
Quote Anchor link
Hallo,

hieronder staat mijn index.html waarop gevraagd wordt naar je huidige locatie, zodra je toestemming heb gegeven verschijnt er een marker op Google Maps met je actuele positie. Daaronder wordt in de DIV "address" je huidige locatie uitgeschreven (Bijv: Your address: Straat, Postcode, Land).

Ik ben opzoek naar de mogelijkheid om het adres uit te lezen in een textbox zonder spaties er tussen.

Heb al verschillende opties geprobeerd maar ik de textbox blijft maar leeg.
Wie kan mij hiermee helpen?


Quote:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-US" lang="en-US">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>HTML 5 Geolocation</title>

<style>
#map {
height:300px;
width:300px;
}

</style>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery", "1"); google.load("jqueryui", "1");</script>

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAiUzO1s6QWHuyzxx-JVN7ABSUL8-Cfeleqd6F6deqY-Cw1iTxhxQkovZkaxsxgKCdn1OCYaq7Ubz3SQ" type="text/javascript"></script>
<script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=n2wY9mzV34Hsdslq6TJoeoJDLmAfzeBamSwJX7jBGLnjM7oDX7fU.Oe91KwUbOwqzvc-"></script>
<script type="text/javascript">

// Geolocation with HTML 5 and Google Maps API based on example from maxheapsize: http://maxheapsize.com/2009/04/11/getting-the-browsers-geolocation-with-html-5/
//
// This script is by Merge Database and Design, http://merged.ca/ -- if you use some, all, or any of this code, please offer a return link.

var map;
var mapCenter
var geocoder;
var fakeLatitude;
var fakeLongitude;

function initialize()
{

if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition( function (position) {

// Did we get the position correctly?
// alert (position.coords.latitude);

// To see everything available in the position.coords array:
// for (key in position.coords) {alert(key)}

mapServiceProvider(position.coords.latitude,position.coords.longitude);

}, // next function is the error callback
function (error)
{
switch(error.code)
{
case error.TIMEOUT:
alert ('Timeout');
break;
case error.POSITION_UNAVAILABLE:
alert ('Position unavailable');
break;
case error.PERMISSION_DENIED:
alert ('Permission denied');
break;
case error.UNKNOWN_ERROR:
alert ('Unknown error');
break;
}
}
);
}
else
{
alert("I'm sorry, but geolocation services are not supported by your browser or you do not have a GPS device in your computer. I will use a sample location to produce the map instead.");

fakeLatitude = 49.273677;
fakeLongitude = -123.114420;

//alert(fakeLatitude+', '+fakeLongitude);
mapServiceProvider(fakeLatitude,fakeLongitude);
}
}

function mapServiceProvider(latitude,longitude)
{
if (window.location.querystring['serviceProvider']=='Yahoo')
{
mapThisYahoo(latitude,longitude);
}
else
{
mapThisGoogle(latitude,longitude);
}
}

function mapThisYahoo(latitude,longitude)
{
var map = new YMap(document.getElementById('map'));
map.addTypeControl();
map.setMapType(YAHOO_MAP_REG);
map.drawZoomAndCenter(latitude+','+longitude, 3);

// add marker
var currentGeoPoint = new YGeoPoint( latitude, longitude );
map.addMarker(currentGeoPoint);

// Start up a new reverse geocoder for addresses?
// YAHOO Ajax/JS/Rest API does not yet support reverse geocoding (though they do support it via Actionscript... lame)
// So we'll have to use Google for the reverse geocoding anyway, though I've left this part of the script just in case Yahoo! does support it and I'm not aware of it yet
geocoder = new GClientGeocoder();
geocoder.getLocations(latitude+','+longitude, addAddressToMap);
}

function mapThisGoogle(latitude,longitude)
{
var mapCenter = new GLatLng(latitude,longitude);
map = new GMap2(document.getElementById("map"));
map.setCenter(mapCenter, 15);
map.addOverlay(new GMarker(mapCenter));

// Start up a new reverse geocoder for addresses?
geocoder = new GClientGeocoder();
geocoder.getLocations(latitude+','+longitude, addAddressToMap);
}

function addAddressToMap(response)
{
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
} else {
place = response.Placemark[0];
$('#address').html('Your address: '+place.address);
}
}

window.location.querystring = (function() {

// by Chris O'Brien, prettycode.org
var collection = {};
var querystring = window.location.search;
if (!querystring) {
return { toString: function() { return ""; } };
}
querystring = decodeURI(querystring.substring(1));

var pairs = querystring.split("&");

for (var i = 0; i < pairs.length; i++) {

if (!pairs) {
continue;
}
var seperatorPosition = pairs.indexOf("=");

if (seperatorPosition == -1) {
collection[pairs] = "";
}
else {
collection[pairs.substring(0, seperatorPosition)]
= pairs.substr(seperatorPosition + 1);
}
}

collection.toString = function() {
return "?" + querystring;
};

return collection;
})();
</script>

</head>
<body onLoad="initialize()">
<div id="content">
<div id="map"></div>
<p id="address"></p>
</div>
</body>
</html>

[/quote]
 
PHP hulp

PHP hulp

28/03/2024 18:24:49
 
Kris Peeters

Kris Peeters

24/01/2012 12:17:55
Quote Anchor link
Code graag tussen code tags, niet tussen quote tags.

Bekijk dit eens:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
  // @see http://www.phphulp.nl/php/forum/topic/huidige-locatie-data-in-een-textbox-weergeven/82209/
  // bv. Oud Sluis, van Sergio Herman
  // standaard-locatie

    $my_marker = array(
    "lat" => 51.307069,
    "lng" => 3.38727,
    "zoom" => 18,
    "title" => "Oud Sluis",
    "maptype" => 'HYBRID'
  );
  
  $sensor = 'true';
  
echo '<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?sensor='
. $sensor . '">
    </script>
    <script type="text/javascript">
      var geocoder;
      var map;
      function initialize() {
        //
        var myLatlng = new google.maps.LatLng('
. $my_marker['lat'] . ', ' . $my_marker['lng'] . ');
        
        var myOptions = {
          center: myLatlng,
          zoom: '
. $my_marker['zoom'] . ',
          mapTypeId: google.maps.MapTypeId.'
. $my_marker['maptype'] . '
        };
 
        map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
        geocoder = new google.maps.Geocoder();
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var myPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            map.setCenter(myPosition);
            var marker = new google.maps.Marker({
              position: myPosition,
              map: map,
              title:"Uw locatie"
            });
            geocoder.geocode({"latLng": myPosition}, getAddress);
          });
        }
      }
      
      function displayAddress(addressArray) {
        document.getElementById("address").value =
            addressArray[0] +" "
          + addressArray[1] +", "
          + addressArray[2] +" "
          + (addressArray[3] != "" ? "(" + addressArray[3] +") " : "")
          + addressArray[4] +" ";
      }
      
      function getAddress(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
              var addressString = "";
              var resultArray = Array("", "", "", "", "");
              for(var i=0; i<results[1].address_components.length; i++) {
                var elm = results[0].address_components[i];
                switch (elm.types[0]) {
                  case "street_number":
                    resultArray[1] = elm.long_name;
                    break;
                  case "route":
                    resultArray[0] = elm.long_name;
                    break;
                  case "locality":
                    resultArray[2] = elm.long_name;
                    break;
                  case "administrative_area_level_2":
                    resultArray[3] = elm.long_name;
                    break;
                  case "administrative_area_level_1":
                    resultArray[4] = elm.long_name;
                    break;
                }
              }
              displayAddress(resultArray);
            }
          } else {
            // alert("Geocoder failed due to: " + status);
          }
       }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:400px; height:400px"></div>
    <input id="address" style="width: 500px;"/>
  </body>
</html>'
;  
?>


Dit doet ongeveer wat je wil, denk ik.

Let vooral op de lijnen tussen 43 en 54.

Verder kan je de functie displayAddress aanpassen naar je wensen.

Krijg je dit geïntegreerd in je eigen site?
Gewijzigd op 24/01/2012 12:19:31 door Kris Peeters
 
Sebas V

Sebas V

24/01/2012 20:34:53
Quote Anchor link
Hee Kris,

ik krijg dit niet geïntegreerd in mijn site, krijg een aantal foutmeldingen in de code. Krijg je deze ook?

Mvg,
Sebastian

Toevoeging op 24/01/2012 20:36:22:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-US" lang="en-US">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>HTML 5 Geolocation</title>

<style>
#map {
height:300px;
width:300px;
}

</style>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery", "1"); google.load("jqueryui", "1");</script>

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAiUzO1s6QWHuyzxx-JVN7ABSUL8-Cfeleqd6F6deqY-Cw1iTxhxQkovZkaxsxgKCdn1OCYaq7Ubz3SQ" type="text/javascript"></script>
<script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=n2wY9mzV34Hsdslq6TJoeoJDLmAfzeBamSwJX7jBGLnjM7oDX7fU.Oe91KwUbOwqzvc-"></script>
<script type="text/javascript">

// Geolocation with HTML 5 and Google Maps API based on example from maxheapsize: http://maxheapsize.com/2009/04/11/getting-the-browsers-geolocation-with-html-5/
//
// This script is by Merge Database and Design, http://merged.ca/ -- if you use some, all, or any of this code, please offer a return link.

var map;
var mapCenter
var geocoder;
var fakeLatitude;
var fakeLongitude;

function initialize()
{

if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition( function (position) {

// Did we get the position correctly?
// alert (position.coords.latitude);

// To see everything available in the position.coords array:
// for (key in position.coords) {alert(key)}

mapServiceProvider(position.coords.latitude,position.coords.longitude);

}, // next function is the error callback
function (error)
{
switch(error.code)
{
case error.TIMEOUT:
alert ('Timeout');
break;
case error.POSITION_UNAVAILABLE:
alert ('Position unavailable');
break;
case error.PERMISSION_DENIED:
alert ('Permission denied');
break;
case error.UNKNOWN_ERROR:
alert ('Unknown error');
break;
}
}
);
}
else
{
alert("I'm sorry, but geolocation services are not supported by your browser or you do not have a GPS device in your computer. I will use a sample location to produce the map instead.");

fakeLatitude = 49.273677;
fakeLongitude = -123.114420;

//alert(fakeLatitude+', '+fakeLongitude);
mapServiceProvider(fakeLatitude,fakeLongitude);
}
}

function mapServiceProvider(latitude,longitude)
{
if (window.location.querystring['serviceProvider']=='Yahoo')
{
mapThisYahoo(latitude,longitude);
}
else
{
mapThisGoogle(latitude,longitude);
}
}

function mapThisYahoo(latitude,longitude)
{
var map = new YMap(document.getElementById('map'));
map.addTypeControl();
map.setMapType(YAHOO_MAP_REG);
map.drawZoomAndCenter(latitude+','+longitude, 3);

// add marker
var currentGeoPoint = new YGeoPoint( latitude, longitude );
map.addMarker(currentGeoPoint);

// Start up a new reverse geocoder for addresses?
// YAHOO Ajax/JS/Rest API does not yet support reverse geocoding (though they do support it via Actionscript... lame)
// So we'll have to use Google for the reverse geocoding anyway, though I've left this part of the script just in case Yahoo! does support it and I'm not aware of it yet
geocoder = new GClientGeocoder();
geocoder.getLocations(latitude+','+longitude, addAddressToMap);
}

function mapThisGoogle(latitude,longitude)
{
var mapCenter = new GLatLng(latitude,longitude);
map = new GMap2(document.getElementById("map"));
map.setCenter(mapCenter, 15);
map.addOverlay(new GMarker(mapCenter));

// Start up a new reverse geocoder for addresses?
geocoder = new GClientGeocoder();
geocoder.getLocations(latitude+','+longitude, addAddressToMap);
}

function addAddressToMap(response)
{
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
} else {
place = response.Placemark[0];
$('#address').html('Your address: '+place.address);
}
}

window.location.querystring = (function() {

// by Chris O'Brien, prettycode.org
var collection = {};
var querystring = window.location.search;
if (!querystring) {
return { toString: function() { return ""; } };
}
querystring = decodeURI(querystring.substring(1));

var pairs = querystring.split("&");

for (var i = 0; i < pairs.length; i++) {

if (!pairs) {
continue;
}
var seperatorPosition = pairs.indexOf("=");

if (seperatorPosition == -1) {
collection[pairs] = "";
}
else {
collection[pairs.substring(0, seperatorPosition)]
= pairs.substr(seperatorPosition + 1);
}
}

collection.toString = function() {
return "?" + querystring;
};

return collection;
})();
</script>

</head>
<body onLoad="initialize()">
<div id="content">
<div id="map"></div>
<p id="address"></p>
</div>
</body>
</html>


De code nogmaals

Toevoeging op 24/01/2012 20:38:27:

Het gaat mij erom dat de adres gegevens getoond in de DIV "address" verplaatst worden en in een textbox verschijnen zodat ik deze data kan gebruiken voor een formulier.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.