0

I want to use Google Map Markers to mark the places where we provide our services. MY Code using Lat/Long is working Fine but in my Project i need to use City Names instead of lat/long. How can i accomplish this is there any way to do this? here is my code

        <div id="map" style="height:500px;"></div>
    <script>
        var neighborhoods = [
          { lat: 31.6400, lng: 74.8600 },
          { lat: 28.6139, lng: 77.2090 },
          { lat: 12.9667, lng: 77.5667 },
          { lat: 31.3260, lng: 75.5760 },
          { lat: 30.7500, lng: 76.7800 },
          { lat: 30.3400, lng: 76.3800 }
        ];

        var markers = [];
        var map;

        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 5,
                center: { lat: 28.6139, lng: 77.2090 }
            });
        }

        function drop() {
            clearMarkers();
            for (var i = 0; i < neighborhoods.length; i++) {
                addMarkerWithTimeout(neighborhoods[i], i * 200);
            }
        }

        function addMarkerWithTimeout(position, timeout) {
            window.setTimeout(function () {
                markers.push(new google.maps.Marker({
                    position: position,
                    map: map,
                    title: 'Jassie',
                    animation: google.maps.Animation.DROP
                }));
            }, timeout);
        }

        function clearMarkers() {
            for (var i = 0; i < markers.length; i++) {
                markers[i].setMap(null);
            }
            markers = [];
        }
        $(window).load(function () {
            drop();
        });
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=API_Key&signed_in=true&callback=initMap">  </script>
</div>
el solo lobo
  • 973
  • 11
  • 23
Anil Kumar
  • 21
  • 1
  • 3
  • possible duplicate of [Using Address Instead Of Longitude And Latitude With Google Maps API](http://stackoverflow.com/questions/15925980/using-address-instead-of-longitude-and-latitude-with-google-maps-api) – geocodezip Feb 24 '16 at 14:12
  • possible duplicate of [Multiple markers Google Map API v3 from array of addresses and avoid OVER_QUERY_LIMIT while geocoding on pageLoad](http://stackoverflow.com/questions/19640055/multiple-markers-google-map-api-v3-from-array-of-addresses-and-avoid-over-query) – geocodezip Feb 24 '16 at 14:16

2 Answers2

0

According to the spec (https://developers.google.com/maps/documentation/javascript/markers), you have to use a long/lat value to add a marker. However, you can use the Google Geocoding API (https://developers.google.com/maps/documentation/geocoding/intro) to translate an address into a long/lat value. So basically your flow becomes:

  • geocode address x
  • take the result and use that in the marker
Raymond Camden
  • 10,661
  • 3
  • 34
  • 68
0

You have to use the Geocoding API. An example to make an Address to Lat/lng:

 doGeocode: function (address, postal_code, callback) {
    console.log("TEST: "+address.toString());
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address,
        'componentRestrictions': {
            'postalCode': postal_code,
            'country': 'de'
        }
    }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            callback(results);
        } else {
            //Error handling
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });

Note: The address can also contain the houseNumber but enter a whitespace.

There is also a reverse Way to get Addresses from Lat/lng. Just in case some needs that:

reverseGeocoder: function (lat, lng, callback) {

    var geocoder = new google.maps.Geocoder;
    var addressComponents = {};
    geocoder.geocode({
        'location': {
            lat:lat,
            lng: lng
        }
    }, function (results, status){
        if(status === google.maps.GeocoderStatus.OK){
            if (results[1]) {
                var result = results[0].address_components;

                for (var i = 0; i < result.length; i++) {
                    if (result[i].types[0] == "route") {
                        console.log(result[i].long_name);
                        addressComponents.route = result[i].long_name
                    }
                    if (result[i].types[0] == "street_number") {
                        console.log(result[i].long_name);
                        addressComponents.street_number = result[i].long_name
                    }
                    if (result[i].types[0] == "locality") {
                        console.log(result[i].long_name);
                        addressComponents.locality = result[i].long_name
                    }
                    if (result[i].types[0] == "postal_code") {
                        console.log(result[i].long_name);
                        addressComponents.postal_code = result[i].long_name
                    }
                }
                callback(addressComponents);
            } else {
                alert('No information found.');
            }

        }else {
            alert("Die geolocation abfrage konnte leider nicht ausgeführt werden. "+status);
        }
    });

From the first function above you will get the Lat/lng. Then simply use a function like this:

var newMarkers = [];        //Global marker array
 marker: function (lat, lng) {
    console.log("new marker");

    //console.log("LAT: "+lat+ "LNG: "+lng);
    var marker = new google.maps.Marker({
        position: {lat: lat, lng: lng},
        icon: icon,
        map: map
    });
    markers.push(marker);
    marker.addListener('click', function () {
        //InfoWindow or whatever u want
    });

    map.setCenter({lat: lat, lng: lng});
    map.setZoom(16);
},
el solo lobo
  • 973
  • 11
  • 23