Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to move the the center position of map with pixcel offset but getting error!!


Excat error below:

Uncaught TypeError: Cannot read property 'fromLatLngToPoint' of undefined


What I have tried:

<pre lang="HTML"><pre><!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
      html, body, #map_canvas { margin: 0; padding: 0; height: 100% ;
      background-image: ellipse at top right,rgba(255,255,255,.2) 67%,#e7e7e7);
      }
    </style>
    <script type="text/javascript" src="  https://maps.googleapis.com/maps/api/js?"></script>
    <script type="text/javascript">
      var map;
      var grayStyles = 
  
[
  {
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#f5f5f5",
        
         
          
      }
    ]
  },
  {
    "elementType": "labels",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#616161"
      }
    ]
  },
  {
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "color": "#f5f5f5"
      }
    ]
  },
  {
    "featureType": "administrative.land_parcel",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#c0c0c0"
      },
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "poi",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#eeeeee"
      }
    ]
  },
  {
    "featureType": "poi",
    
    "stylers": [
      { "visibility": "off" }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#e5e5e5"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "labels.icon",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "stylers": [
      { "visibility": "off" }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "color": "#9e9e9e"
      },
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#ffffff"
      }
    ]
  },
  {
    "featureType": "road.arterial",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#757575"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#dadada"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "labels.icon",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#616161"
      },
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "road.local",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#9e9e9e"
      }
    ]
  },
  {
    "featureType": "transit.line",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#e5e5e5"
      }
    ]
  },
  {
    "featureType": "transit.station",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#eeeeee"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#c9c9c9"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#9e9e9e"
      }
    ]
  }
];
      var mapOptions = {
        center: new google.maps.LatLng(41.327670, 19.8029607),
        zoom: 18,
        styles: grayStyles,
        mapTypeControl: false,
      disableDefaultUI: true,
       
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };

      function initialize() {
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
      
        var cityCircle = new google.maps.Circle({
            strokeColor: '#000000',
            strokeOpacity: 0.0,
            strokeWeight: 10,
            fillColor: '#000000',
            fillOpacity: 0.9 ,
            map: map,
            center: new google.maps.LatLng(41.327670, 19.8029607),
            radius: 1
          });
        var cityCircle = new google.maps.Circle({
            strokeColor: '#bfbfbf',
            strokeOpacity: 0.5,
            strokeWeight: 1,
            fillColor: '#bfbfbf',
            fillOpacity: 0.20,
            map: map,
            center: new google.maps.LatLng(41.327670, 19.8029607),
            radius: 10
          });
        var cityCircle = new google.maps.Circle({
            strokeColor: '#bfbfbf',
            strokeOpacity: 0.5,
            strokeWeight: 1,
            fillColor: '#bfbfbf',
            fillOpacity: 0.20,
            map: map,
            center: new google.maps.LatLng(41.327670, 19.8029607),
            radius: 20
          });
        var cityCircle = new google.maps.Circle({
            strokeColor: '#bfbfbf',
            strokeOpacity: 0.5,
            strokeWeight: 1,
            fillColor: '#bfbfbf',
            fillOpacity: 0.20,
            map: map,
            center: new google.maps.LatLng(41.327670, 19.8029607),
            radius: 30
          });
          updatemarkerposition(new google.maps.LatLng(41.327670, 19.8029607),225,1117);
      }

    function updatemarkerposition (latlng, offsetx, offsety) {
                    
                   
                    var scale = Math.pow(2, 16);
                    //var nw = new google.maps.LatLng(
                    //    map.getBounds().getNorthEast().lat(),
                    //    map.getBounds().getSouthWest().lng());
                    var worldCoordinateCenter = map
                        .getProjection()
                        .fromLatLngToPoint(marker.getPosition());
                    var pixelOffset = new google.maps.Point(
                        offsetx / scale || 0,
                        offsety / scale || 0
                    );

                    var worldCoordinateNewCenter = new google.maps.Point(
                        worldCoordinateCenter.x - pixelOffset.x,
                        worldCoordinateCenter.y + pixelOffset.y
                    );

                    var newCenter = map
                        .getProjection()
                        .fromPointToLatLng(worldCoordinateNewCenter);
                    console.log(newCenter);
                    $.productmap.map.setCenter(newCenter);
                }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    
    <div id="map_canvas" >
      <p>heloo</p>
    </div>
    
  </body>
</html>
Posted
Updated 1-Jan-19 16:49pm
Comments
Richard MacCutchan 1-Jan-19 13:13pm    
The variable named map has not been initialised.
Nawab Ahmad 1-Jan-19 21:34pm    
map is global variable and initialized at the top,please run the code and then answer.

1 solution

Try update the code to call the updatemarkerposition function by listening to projection_changed event to ensure map is not null / initialized.

JavaScript
google.maps.event.addListenerOnce(map,"projection_changed", function() {
  updatemarkerposition(new google.maps.LatLng(41.327670, 19.8029607),225,1117);
 });

Also, this line is kind of odd, maybe you didn't paste all the code. I don't see the marker object defined in the posted code.
JavaScript
.fromLatLngToPoint(marker.getPosition());


Resources: Maps  |  Maps JavaScript API  |  Google Developers[^]
 
Share this answer
 
Comments
Nawab Ahmad 1-Jan-19 23:22pm    
Thanks for update, above code is working code, please copy and paste to code pen it will work ..
Bryian Tan 1-Jan-19 23:41pm    
Here is an example using the posted code and above code snippet to get rid of the error "Uncaught TypeError: Cannot read property 'fromLatLngToPoint' of undefined"

jQuery addClass example - JSFiddle[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900