简体   繁体   中英

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

Details

  • I want to convert address to coordinates
  • I want to use that coordinate to populate a map view
  • I know my $coordinate variable = 42.6358311,-71.3290596
  • I got that by simply running alert( coordinates );

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

在此处输入图片说明

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.

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>

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM