Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This is my google map code. I'm trying to update new latitude and longitude when i moved the existing marker to new position. But I don't understand why the following google map code is not updating the new latitude and longitude when i moved it to new position. Can you tell me what is the wrong in my code?

<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api 
/js?sensor=false"></script>
<script type="text/javascript">
$(document).ready(function() {

var mapCenter = new google.maps.LatLng(23.709920999999998,90.40714299999999); //Google map Coordinates
//var mapCenter = new google.maps.LatLng(48.856614,2.352222); //Google map Coordinates

var map;

map_initialize(); // initialize google map

//############### Google Map Initialize ##############
function map_initialize()
{
        var googleMapOptions = 
        { 
            center: mapCenter, // map center
            zoom: 16, //zoom level, 0 = earth view to higher value
            maxZoom: 22,
            minZoom: 2,
            zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL //zoom control size
        },
            scaleControl: true, // enable scale control             
            mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
        };

        map = new google.maps.Map(document.getElementById("google_map"), googleMapOptions);         

        //Load Markers from the XML File, Check (map_process.php)
        $.get("map_process.php", function (data) {
            $(data).find("marker").each(function () {
                  var name      = $(this).attr('name');
                  var id        = $(this).attr('locId');
                  var uid       = $(this).attr('userid');
                  var address   = '<p>'+ $(this).attr('address') +'</p>';
                  var type      = $(this).attr('type');
                  var point     = new google.maps.LatLng(parseFloat($(this).attr('latitude')),parseFloat($(this).attr('longitude')));
                  create_marker(point, name, id, uid, address, false, false, false, "icons/blue-icon.png");
            });
        });

        //Right Click to Drop a New Marker
        google.maps.event.addListener(map, 'rightclick', function(event) {
            //Edit form to be displayed with new marker
            var EditForm = '<p><div class="marker-edit">'+
            '<form action="ajax-save.php" method="POST" name="SaveMarker" id="SaveMarker">'+
            '<label for="pName"><span>Place Name :</span><input type="text" name="pName" class="save-name" placeholder="Enter Title" maxlength="40" /></label>'+
            '<label for="pDesc"><span>Description :</span><textarea name="pDesc" class="save-desc" placeholder="Enter Address" maxlength="150"></textarea></label>'+                
            '</form>'+
            '</div></p><button name="save-marker" class="save-marker">Save Marker Details</button>';
            //Drop a new Marker with our Edit Form
            create_marker(event.latLng, 'New Marker',id=0, uid =0, EditForm, true, true, true, "icons/purple.png");
        });

}

//############### Create Marker Function ##############
function create_marker(MapPos, MapTitle, idd, uid, MapDesc,  InfoOpenDefault, DragAble, Removable, iconPath)
{                 

    //new marker
    var marker = new google.maps.Marker({
        position: MapPos,
        map: map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        title:"Hello World!",
        icon: iconPath
    });

    //Content structure of info Window for the Markers
    var contentString = $('<div class="marker-info-win">'+
    '<div class="marker-inner-win"><span class="info-content">'+
    '<h1 class="marker-heading">'+MapTitle+'</h1>'+
    MapDesc+ 

    '</span><button name="remove-marker" class="remove-marker" title="Remove Marker">Remove Marker</button>'+
    '</span><button name="update-marker" class="update-marker" title="Update Marker">Update Marker</button>'+               
    '</div></div>');    

    //Create an infoWindow
    var infowindow = new google.maps.InfoWindow();
    //set the content of infoWindow
    infowindow.setContent(contentString[0]);

    //Find remove button in infoWindow
    var removeBtn   = contentString.find('button.remove-marker')[0];
    var saveBtn     = contentString.find('button.save-marker')[0];
    var updateBtn   = contentString.find('button.update-marker')[0];

    //add click listner to remove marker button
    google.maps.event.addDomListener(removeBtn, "click", function(event) {
        remove_marker(marker);
    });
    //add click listner to remove marker button
    google.maps.event.addDomListener(updateBtn, "dragend", function(event) {
        update_marker(marker,MapTitle, idd, uid, MapDesc);
    });

    if(typeof saveBtn !== 'undefined') //continue only when save button is present
    {
        //add click listner to save marker button
        google.maps.event.addDomListener(saveBtn, "click", function(event) {
            var mReplace = contentString.find('span.info-content'); //html to be replaced after success
            var mName = contentString.find('input.save-name')[0].value; //name input field value
            var mDesc  = contentString.find('textarea.save-desc')[0].value; //description input field value
            var mType = contentString.find('select.save-type')[0].value; //type of marker
            var psType = contentString.find('select.save-pstype')[0].value; //type of marker

                save_marker(marker, mName, mDesc, mType, psType, mReplace); //call save marker function

        });
    }

    //add click listner to save marker button        
    google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker); // click on marker opens info window 
    });

    if(InfoOpenDefault) //whether info window should be open by default
    {
      infowindow.open(map,marker);
    }
}   

function update_marker(Marker, mName, idd, uid, mAddress)
{
    //Save new marker using jQuery Ajax
    //alert(mName);
    var mLatLang = Marker.getPosition().toUrlValue(); //get marker position
    var myData = {update : 'true',name : mName, address : mAddress, id:idd, userid: uid,latlang : mLatLang }; //post variables
    //console.log(replaceWin);      
    $.ajax({
      type: "POST",
      url: "map_update.php",
      data: myData,
      success:function(data){
            //replaceWin.html(data); //replace info window with new html
            Marker.setDraggable(true); //set marker to fixed
            Marker.setIcon('icons/blue_icon.png'); //replace icon
            //alert(data);
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError); //throw any errors
        }
    });
}

});
</script>
share|improve this question
    
I found it....yea..... –  creativeartbd Nov 5 '13 at 14:14
    
map.panTo(laLatLng); stackoverflow.com/questions/14306098/… –  Mapperz Nov 5 '13 at 14:31
1  
@creativeartbd, if you now have the solution to your answer, please consider posting an answer to your own question. –  Andre Silva Nov 5 '13 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.