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

后端 未结 4 1475
闹比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:14
    <?php
        /* lat/lng data will be added to this array */
        try{$work=$_GET["service"];}catch(Exception $e){echo 'Authorization Failed.Map may Misbehave or Buggy.<a href="submitbug.php">Click here to report this</a>';}
        $locations=array();
        $uname="root";
        $pass="";
        $servername="localhost";
        $dbname="bcremote";
        $db=new mysqli($servername,$uname,$pass,$dbname);
        $query =  $db->query('SELECT * FROM location');
        while( $row = $query->fetch_assoc() ){
            $name = $row['uname'];
            $longitude = $row['longitude'];                              
            $latitude = $row['latitude'];
            /* Each row is added as a new array */
            $locations[]=array( 'name'=>$name, 'lat'=>$latitude, 'lng'=>$longitude );
        }
        //echo $locations[0]['name'].": In stock: ".$locations[0]['lat'].", sold: ".$locations[0]['lng'].".<br>";
        //echo $locations[1]['name'].": In stock: ".$locations[1]['lat'].", sold: ".$locations[1]['lng'].".<br>";
    ?>
    <script>
           //var myLatLng = {lat: -25.363, lng: 131.044};
           function initMap() {
            var myLatLng = {lat: -25.363, lng: 131.044};
    
            var map = new google.maps.Map(document.getElementById('map'), {
              center: myLatLng,
              scrollwheel: false,
              zoom: 4
             });
    
            <?php for($i=0;$i<sizeof($locations);$i++)
            { ?>
             var marker = new google.maps.Marker({
              map: map,
              position: {lat: <?php echo $locations[$i]['lat']?>,lng: <?php echo $locations[$i]['lng']?>},
              title: 'Service'
            });
            <?php } ?>
           }
    </script>
    

    This code Snippet uses Dynamic Content without using JSON or XML. This below code is what we see in the Browser View Source tool. Hope this helps you...

    <script>
           //var myLatLng = {lat: -25.363, lng: 131.044};
           function initMap() {
            var myLatLng = {lat: -25.363, lng: 131.044};
    
            var map = new google.maps.Map(document.getElementById('map'), {
              center: myLatLng,
              scrollwheel: false,
              zoom: 4
             });
    
                     var marker = new google.maps.Marker({
              map: map,
              position: {lat: 192.652.231.25,lng: 192.652.231.25},
              title: 'Service'
            });
                     var marker = new google.maps.Marker({
              map: map,
              position: {lat: 192.652.231.25,lng: 192.652.231.25},
              title: 'Service'
            });
                   }
    </script>
    
    0 讨论(0)
  • 2021-01-22 08:17

    UPDATE 2017

    Google maps now supports the creation of maps with multiple locations which you can embed via iframe! Source: https://www.create.net/support/218-how-to-pin-point-multiple-locations-on-google-maps.html

    1. Go to https://www.google.com/maps

    2. Make sure you're signed in - you can do so by clicking the Login button in the top-right corner.

    3. In the top left corner, next to the search box, click the menu icon to expand the menu

    4. Click "Your Places", "Maps" and then click "Create Map" to edit your map.

    5. A new window will pop up. Give your map a title and description, then click "Save".

    6. You can now pinpoint locations manually by clicking the marker icon and placing it directly onto the map, or search for locations using the search box at the top of the screen.

    7. If you're adding locations manually, you can name the location and save to add it to the map. If you're searching and adding specific locations, a green marker will appear on the map and you can click the 'Add to map' link.

    8. Repeat steps 6 and 7 for each location you wish to plot.

    Once you have done that save your map again and refresh the page. Then, to get the code to embed your map on to your Create website, please follow these steps:

    1. Make sure you map is public. You can do this by clicking 'Share' beneath the map name.

    2. Under 'Who has access' click 'Change' and make turn it 'On - Public on the web' and save.

    3. Next, click the menu icon and click on the link 'Embed on my site' The code will then pop up in a new window.

    4. To use this, you will need to paste the code into an HTML fragment on your Create account, and then place the HTML fragment on your chosen page.

    Example:

    <iframe src="https://www.google.com/maps/d/embed?mid=1tX88xmVMIFW9DQneDff_ZMJtekc" width="100%" height="480"></iframe>

    0 讨论(0)
  • 2021-01-22 08:22

    This should work for you assuming lat and lon are javascript arrays with the same length:

    var map = null;
    function initialize() {
    
    var lat='<?php echo $Latitude?>';
    var lon='<?php echo $Longitude?>';
    // initialize map center on first point
    var latlng = new google.maps.LatLng(lat[0],lon[0]);
    var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var bounds = new google.maps.LatLngBounds();
    
    for (var i=0, i<lat.length; i++) {
      var latlng = new google.maps.LatLng(lat[i],lon[i]);
      bounds.extend(latlng);
      var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: "Hello World!"
      });
    }
      // zoom and center the map to show all the markers
      map.fitBounds(bounds);
    }
    
    0 讨论(0)
  • 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);
                    });
                }
            }
        <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA7IZt-36CgqSGDFK8pChUdQXFyKIhpMBY&sensor=true" type="text/javascript"></script>
    
        <div id="map-canvas" style="width: 800px; height: 500px;">
        </div>

    0 讨论(0)
提交回复
热议问题