1

I want to be able to keep a marker constantly in the center of Google Maps on my mobile application.

I have tried using bindTo functionality of Google Maps, which binds the marker to the center of the map, but it seems a bit clumsy.

I have tried doing it using HTML and CSS also, but Google Maps does not allow it.

The same as in Uber app.

asked Jan 12, 2014 at 16:59
3

1 Answer 1

4

This is a fully functional code which may satisfy your requirements:

<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
 <style type="text/css">
 html { height: 100% }
 body { height: 100%; margin: 0; padding: 0 }
 #map-canvas { height: 100% }
 </style>
 <script type="text/javascript"
 src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
 </script>
 <script type="text/javascript">
 function initialize() {
 var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
 var mapOptions = {
 center: myLatlng,
 zoom: 8
 };
 var map = new google.maps.Map(document.getElementById("map-canvas"),
 mapOptions);
 var marker = new google.maps.Marker({
 position: myLatlng,
 map: map,
 title: 'Hello World!'
 });
 google.maps.event.addListener(map, 'center_changed', function() {
 // 0.1 seconds after the center of the map has changed,
 // set back the marker position.
 window.setTimeout(function() {
 var center = map.getCenter();
 marker.setPosition(center);
 }, 100);
 });
 }
 google.maps.event.addDomListener(window, 'load', initialize);
 </script>
</head>
<body><div id="map-canvas"/></body></html>
answered Jan 12, 2014 at 18:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.