Search code examples
javascriptgoogle-maps-api-3google-maps-markers

In google maps javascript api - how to move map not marker


currently i have a little project that needs to make marker static and move map to the marker display on the map - i hope it makes any sense -

how to move map not marker - like i said the marker cannot move but the map needs to move to the marker when location changed - also marker is displayed at the bottom the map to make room for other events above the marker such as a path display and etc -

whats done already is when car moves it watches gps - then tells marker to move to that gps as a current location and then panby to the bottom of the map - this makes marker jerk at the bottom of the map when its location changed - so i figured if i keep marker static by placing it into a div above map and fixate it via css style - so it stays in the same place all the time - but this is wrong as it cannot be moved with map then when someone drags map with a finger - so this cannot be right solution - what is your solution to this dilema unless i am missing something -


Solution

  • To achieve what you want, you need to rotate the map on the correct axis, find the position of the center of the map, and finally center the map at the right place.

    1 - Rotate the map

    In your case (I presume you want the car always face of the road in front of the user position) you need to rotate the map before centering, otherwise the map is always oriented to the north and it can be anoying when the car move in other orientation (south for exemple). In this case the point of view is oriented in the wrong direction and the user don't see the upcoming road.

    You can find an exemple here in the documentation. Basicaly you need to use setHeading() method. The rotation on gmap is not easy than it can be (see this post).

    2 - Find the center of the map

    To find the updated position of the center of your map, you need to retrieve the latLng location of a point always at the same distance (in pixel of the map container) above your marker fixed location. Use the methods fromLatLngToPoint() and fromPointToLatLng() to retrieve the position from the map to the real world or vice versa (see getProjection() for more details).

    Exemple :

    Your map is displayed in 1000*1000 px and the bottom position of the marker you choose is at 500px of the left side and 950px of the top side. If you want to move the center of the map without changing the position of the marker, you need to find the latLng position of the point at 450px above your marker.

    function findMapCenter(map, markerLatLng) {
        // need this to adjust from the actual zoom on the map
        let scale = Math.pow(2, map.getZoom());
        // position in pixel x.y from the container
        let markerWorldCoordinate = map.getProjection().fromLatLngToPoint(markerLatLng);
        // calcul of the position of the center
        let centerWorldCoordinate = new google.maps.Point(
            markerWorldCoordinate.x,
            markerWorldCoordinate.y + (450/scale) // 450 because in my exemple is from 450px at the top of the marker
        );
        // return latLng of the center point of the map
        return map.getProjection().fromPointToLatLng(centerWorldCoordinate);
    }
    

    3 - Set the map at the right place

    Use setCenter() method to center the map on a choosen latLng location.

    let marker; // = config of your marker
    let gMap = new google.maps.Map(document.getElementById('map'));
    update({lat:MARKER_LATITUDE, lng:MARKER_LONGITUDE});
    
    // call this every time you want to update
    function update(markerNewLatLng){
        // ... first step : rotate the map if you need to
        // then update marker position
        marker.setPosition(markerNewLatLng);
        // center the map
        gMap.setCenter(findMapCenter(gmap, markerNewLatLng));
    }