简体   繁体   中英

Google maps API V3 - Two location with markers on same map

I want to display two different location on map at same time, one is current location and other one is different location. I use javascript V3 version. This is my code for display one location.

         var pos = new google.maps.LatLng(latitude, longitude);


             window.localStorage.setItem("event_location",pos);

             if (!google) {
                    loadScript();
                }
             var pos1 = window.localStorage.getItem("current_location");
                alert('position '+pos1);

                var myOptions = {
                    center : pos,
                    zoom : 8,
                    mapTypeId : google.maps.MapTypeId.ROADMAP
                };


                var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

                var contentString = '<div id="content">'+
                '<div id="siteNotice">'+
                '</div>'+
                '<h4 id="firstHeading" class="firstHeading">'+add+'</h4> ' +
                '<div id="bodyContent">'+
                '<p>Meeting with'+'<b> '+ personName + '</b>, <br/>on ' +
                '<b>'+startDate+'</b> at ' + 
                '<b>'+startTime+'</b>' + 
                '</div>'+
                '</div>';


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

            var marker = new google.maps.Marker({
                position: pos,
                map: map,
                title:"Event Location!"
            });



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

pos1 is my second location which is calculated by phonegap current location method. Please help me out.

If pos1 is google.maps.LatLng instance

var marker = new google.maps.Marker({
    position: pos,
    map: map,
    title:"Event Location!"
});

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

var marker1 = new google.maps.Marker({
    position: pos1,
    map: map,
    title:"New Location!"
});

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

marker.setMap(map);
marker1.setMap(map);

write the code for displaying the second marker ie if u have the lat long for pos1 create a marker and display it on the same map. it will hardly take milliseconds to display the secod marker if the client browser has got reasonable processing capacity

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