-1

I want to change map marker position on basis of dropdown change even,What I'm doing is I get lat,long on dropdown event and want to pass these coordinates to my current marker , this is my code

$("#location").change(function () {
    var addr = ($('#location').val());

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': addr }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            alert("location : " + results[0].geometry.location.lat() + " " + results[0].geometry.location.lng());
            geoMarker.setMarkerOptions({
                duration: duration,
                easing: $('#easingOption').val()
            });
        } else {
            alert("Something got wrong " + status);
        }
    });
});

HTML :

<select id="location">
    <option>Dubai</option>
    <option>Sharjah</option>
</select>

It alerts current coordinates but I need to know how do I pass these coordinates to my marker position

Ibrahim Khan
  • 20,280
  • 7
  • 39
  • 53
Sikander
  • 2,718
  • 10
  • 44
  • 90
  • 1
    what is `geoMarker` ? – Dr.Molle Mar 20 '16 at 13:05
  • Please provide a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that demonstrates your issue. – geocodezip Mar 20 '16 at 15:10
  • Related question: [Change google map location on selectbox change after the map and markers loaded](http://stackoverflow.com/questions/34466165/change-google-map-location-on-selectbox-change-after-the-map-and-markers-loaded) – geocodezip Mar 20 '16 at 15:34

2 Answers2

0

Looks like you are looking for .setPosition():

var latlng = new google.maps.LatLng(-24.397, 140.644);
marker.setPosition(latlng);
Undefitied
  • 737
  • 4
  • 14
0

You need to set the position of the marker based on the results of the geocode operation.

$("#location").change(function() {
  var addr = ($('#location').val());

  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({'address': addr
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      geoMarker.setPosition(results[0].geometry.location);
    } else {
      alert("Something got wrong " + status);
    }
  });
});

proof of concept fiddle

code snippet:

var geocoder;
var map;
var geoMarker;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  geoMarker = new google.maps.Marker();
  geoMarker.setPosition(map.getCenter());
  geoMarker.setMap(map);

  $("#location").change(function() {
    var addr = ($('#location').val());

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
      'address': addr
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        geoMarker.setPosition(results[0].geometry.location);
      } else {
        alert("Something got wrong " + status);
      }
    });
  });

}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<select id="location">
  <option>Dubai</option>
  <option>Sharjah</option>
</select>
<div id="map_canvas"></div>
geocodezip
  • 153,563
  • 13
  • 208
  • 237