How to show multiple areas by location in google maps using php

后端 未结 4 1577
闹比i
闹比i 2021-01-22 07:59

Here is the Initialise function.....

        function initialize() {

Here the variables $Latitude,$Longitude are array values so how can i stor

4条回答
  •  庸人自扰
    2021-01-22 08:25

    Its my sample code to plot multiple areas in google map by using area name or lat,lng.

            var map;
            var geocoder;
            var marker;
            var people = new Array();
            var latlng;
            var infowindow;
    
            $(document).ready(function() {
                ViewCustInGoogleMap();
            });
    
            function ViewCustInGoogleMap() {
    
                var mapOptions = {
                    center: new google.maps.LatLng(11.0168445, 76.9558321),   // Coimbatore = (11.0168445, 76.9558321)
                    zoom: 7,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    
                // Get data from database. It should be like below format or you can alter it.
    
                var data = '[{ "DisplayText": "adcv", "ADDRESS": "Jamiya Nagar Kovaipudur Coimbatore-641042", "LatitudeLongitude": "10.9435131,76.9383790", "MarkerId": "Customer" },{ "DisplayText": "abcd", "ADDRESS": "Coimbatore-641042", "LatitudeLongitude": "11.0168445,76.9558321", "MarkerId": "Customer"}]';
    
                people = JSON.parse(data); 
    
                for (var i = 0; i < people.length; i++) {
                    setMarker(people[i]);
                }
    
            }
    
            function setMarker(people) {
                geocoder = new google.maps.Geocoder();
                infowindow = new google.maps.InfoWindow();
                if ((people["LatitudeLongitude"] == null) || (people["LatitudeLongitude"] == 'null') || (people["LatitudeLongitude"] == '')) {
                    geocoder.geocode({ 'address': people["Address"] }, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                            marker = new google.maps.Marker({
                                position: latlng,
                                map: map,
                                draggable: false,
                                html: people["DisplayText"],
                                icon: "images/marker/" + people["MarkerId"] + ".png"
                            });
                            //marker.setPosition(latlng);
                            //map.setCenter(latlng);
                            google.maps.event.addListener(marker, 'click', function(event) {
                                infowindow.setContent(this.html);
                                infowindow.setPosition(event.latLng);
                                infowindow.open(map, this);
                            });
                        }
                        else {
                            alert(people["DisplayText"] + " -- " + people["Address"] + ". This address couldn't be found");
                        }
                    });
                }
                else {
                    var latlngStr = people["LatitudeLongitude"].split(",");
                    var lat = parseFloat(latlngStr[0]);
                    var lng = parseFloat(latlngStr[1]);
                    latlng = new google.maps.LatLng(lat, lng);
                    marker = new google.maps.Marker({
                        position: latlng,
                        map: map,
                        draggable: false,               // cant drag it
                        html: people["DisplayText"]    // Content display on marker click
                        //icon: "images/marker.png"       // Give ur own image
                    });
                    //marker.setPosition(latlng);
                    //map.setCenter(latlng);
                    google.maps.event.addListener(marker, 'click', function(event) {
                        infowindow.setContent(this.html);
                        infowindow.setPosition(event.latLng);
                        infowindow.open(map, this);
                    });
                }
            }
        
        
    
        

提交回复
热议问题