Javascript Google Maps API - Marker无法显示

时间:2015-04-23 09:48:28

标签: javascript google-maps google-maps-api-3 google-maps-markers

我在单独的.js文件中有以下代码,但由于某些原因,制作者在我加载页面时没有显示,只显示地图。

document.addEventListener('DOMContentLoaded', drawMap);  

//MAP

function drawMap() { 

    var myLatLong = new google.maps.LatLng(53.4680477, -2.2400482); 

        var mapOptions = {
            center: myLatLong, 
          zoom: 14 
        };
        var map = new google.maps.Map(document.getElementById('map-canvas'), 
            mapOptions);
      }

//MARKERS

var marker = new google.maps.Marker( 
    {
        position: myLatLong, 
        map: map,
        title: "Marker"
    }
);

marker.setMap(map); 

2 个答案:

答案 0 :(得分:0)

我注意到在初始化地图之前添加了 marker.setMap(map); 。 请尝试以下方法:

package common;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPMessage;

public class SimpleMail{

    /**
           Outgoing Mail (SMTP) Server
           requires TLS or SSL: smtp.gmail.com (use authentication)
           Use Authentication: Yes
           Port for SSL: 465
         */
        public static void main(String[] args) {

             String to="mymailid@gmail.com";
             String subject="New Mail";

             String msg="test test";

            final String user="gmailuser";
            final String pass="gmailpassowd";
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587"); //this is optional
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");



            Session session = Session.getInstance(props,new javax.mail.Authenticator() {

                    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,pass);
           }
           });

            try {
           MimeMessage message = new MimeMessage(session);
           message.setFrom(new InternetAddress(user));
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
           message.setSubject(subject);
           message.setText(msg);

             Transport.send(message);
                System.out.println("Mail sent..");
       }catch(Exception e)
       {
           System.out.println(e);
       }
    }



}

以下是Google developers doc

提供的示例
function drawMap() {
  // init maps then add marker to map
  marker.setMap(map);
}

希望这有帮助。

答案 1 :(得分:-1)

试试这个

document.addEventListener('DOMContentLoaded', drawMap);

//MAP

function drawMap() {

    var myLatLong = new google.maps.LatLng(53.4680477, -2.2400482);

    var mapOptions = {
        center: myLatLong,
        zoom: 14
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

    //MARKERS

    var marker = new google.maps.Marker({
        position: myLatLong,
        map: map,
        title: "Marker"
    });

    marker.setMap(map);
}