Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

javascript - How to Open email application while clicking on the email ID in google Maps InfoWindow

I want to Open the email application with prepopulated subject 'SUBJECT' while clicking on the email ID in the google Maps Info window. Below is the code i wrote to Display the email ID and I am not sure how to proceed further.

function init(resp) {

        var mapProp = {
            center : new google.maps.LatLng(resp.data[0].Lat,
                    resp.data[0].Lon),
            zoom : 7,
        };
        var map = new google.maps.Map(document.getElementById("googleMap"),
                mapProp);

        $.each(resp.data, function(index, value) {

            var myLatlng = new google.maps.LatLng(value.Lat, value.Lon);

            var marker = new google.maps.Marker({
                position : myLatlng,
                label : value['Number'],
                content : "Phone: " + value['Phone Number'] + '<br>'
                        + "E-Mail: " + '<a href="'+ value['E-Mail']+'">'+ value['E-Mail'] +'</a>'
            });

            var infowindow = new google.maps.InfoWindow({
                content : "Phone: " + value['Phone Number'] + '<br>'
                        + "E-Mail: " + '<a href="'+ value['E-Mail']+'">'+ value['E-Mail'] +'</a>'

            });

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map, marker);

            });

            // To add the marker to the map, call setMap();
            marker.setMap(map);

        });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the mailto of JavaScript. You can change the values of different parameters to suit your email.

So in your code, you can put something like this as your content in your InfoWindow:

  var infowindow = new google.maps.InfoWindow({
    content :'<div id="content">'+    
      '<a href="mailto:username@example.com?subject=Subject&body=message%20goes%20here">'+
      'Click to Email</a> '+
      '</div>'
  });

The following is a sample code. To see how this works, you can copy and save it as html file and run it in your browser. Please note to change your API Key:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info Windows</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

      // This example displays a marker at the center of Australia.
      // When the user clicks the marker, an info window opens.

      function initMap() {
        var uluru = {lat: -25.363, lng: 131.044};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: uluru
        });

       var contentString = '<div id="content">'+
      '<a href="mailto:username@example.com?subject=Subject&body=message%20goes%20here">'+
      'Click to Email</a> '+
      '</div>';

        var infowindow = new google.maps.InfoWindow({
          content: contentString
        });

        var marker = new google.maps.Marker({
          position: uluru,
          map: map,
          title: 'Uluru (Ayers Rock)'
        });
        marker.addListener('click', function() {
          infowindow.open(map, marker);
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=CHANGE_API_KEY_HERE&callback=initMap">
    </script>
  </body>
</html>

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...