iori
iori

Reputation: 3506

How to print out my Javascript variable as part of my HTML attribute?

Details

What I have now

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var geocoder = new google.maps.Geocoder();
var address = "410 walker street Lowell MA 01851";

geocoder.geocode( { 'address': address}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) {

    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();

    var coordinates = [latitude , longitude];



    alert( coordinates );


    } 
}); 
</script>

Note : 410 walker street Lowell MA 01851 -> 42.6358311,-71.3290596

<iframe

    width="200"
    height="200"
    frameborder="0" style="border:0"
    src="https://www.google.com/maps/embed/v1/view?key=**********

        &center= 42.6358311,-71.3290596
        &zoom=18
        &maptype=satellite"

  >
  </iframe>

If I run this code I will get this result

enter image description here

So far everything work, you might wondering, since everything work why am I asking ?

Here is my question, rather than inputing the latitude and the longitude manually in &center= 42.6358311,-71.3290596 I was wondering, if I can print out whatever stored inside my $coordinate variable.

Can someone please help me how to do that ? I just want to print out my $coordinate variable as part of my HTML attribute.

Upvotes: 0

Views: 152

Answers (2)

Eko Junaidi Salam
Eko Junaidi Salam

Reputation: 1681

Try this code snippet, may be this help you...

var geocoder = new google.maps.Geocoder();
var address = "410 walker street Lowell MA 01851";

geocoder.geocode({
    'address': address
}, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();
        var coordinates = [latitude , longitude];
        var mapOptions = {
            zoom: 18,
            center: new google.maps.LatLng(latitude, longitude),
            mapTypeId: google.maps.MapTypeId.SATELLITE
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);        
        document.getElementById('myText').value = coordinates;
    }
});
<script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<div id="map_canvas" style="width:500px; height:200px"></div>
<br />
<p>Print out your coordinate : </p><input type="text" id="myText" >

do you want to print out like this ? Please correct me if I'm wrong..

Upvotes: 1

geocodezip
geocodezip

Reputation: 161404

See the documentation

"Highlight a place or an address" gives me this result:

<iframe width="600" height="450" frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?q=410+walker+street+Lowell+MA+01851
&key=..."></iframe>

Upvotes: 1

Related Questions