点击谷歌地图上的标记添加信息窗口

时间:2017-03-06 13:56:30

标签: javascript php mysql google-maps

我正在使用PHP和MYSQL从数据库中检索数据并在Google Map API上显示标记,并添加一个信息窗口,其中包含使用click-Listener从数据库中检索到的信息。  在我添加信息窗口后,它似乎不起作用。

我的错误在哪里?

代码:

<?php
        /*
        Template Name: MAP2
        */

        get_header();
  ?>


<!DOCTYPE html>
<html>
  <head>
    <title>Custom Markers</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCquey2tCZ32jLJJDEEi2D7_RnXXyj9RTI&callback=initMap">
    </script>
     <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 600px;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>

  </head>
  <body>
    <div id="map">
      <div class="map-popupstring hidden" id="popupstring1">
                <div class="text-center">
                    <h3>title</h3>
                     <p>subinfo</p>
                </div>
            </div>

    </div>

    <script>

     var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 8,
          center: new google.maps.LatLng(33.888630, 35.495480),
          mapTypeId: 'roadmap'
        });

        var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
        var icons = {
          parking: {
            icon: iconBase + 'parking_lot_maps.png'
          },
          library: {
            icon: iconBase + 'library_maps.png'
          },
          info: {
            icon: iconBase + 'info-i_maps.png'
          }
        };


        var $popup = $("#popupstring1")

        function addMarker(feature) {
          var marker = new google.maps.Marker({
            position: feature.position,
            //icon: icons[feature.type].icon,
            data-popupstring-id: $popup,
            map: map
          });
        }



        var features = [
        <?php
          global $wpdb;
            $prependStr ="";
            foreach( $wpdb->get_results("SELECT siteID, latitude, longitude FROM site_coordinates2", OBJECT) as $key => $row) {
               $latitude = $row->latitude;
               $longitude = $row->longitude;
               $info = $row->siteID;
           echo $prependStr;
       ?>
{
    position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
    type: '<?php echo $info; ?>'
}
<?php
$prependStr =",";
}
?>
        ];

    var infowindow = new google.maps.InfoWindow();

        for (var i = 0, feature; feature = features[i]; i++) {

          addMarker(feature);
        }
}



         </script>


  </body>
</html>

<?php
get_footer();
?>

2 个答案:

答案 0 :(得分:1)

package com.marconota.serieasoccerscorekeeper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.commons.lang3.StringUtils;
import java.lang.String;


public class MainActivity extends AppCompatActivity {
    String scorerNames = "Marcatori: ";
    String lastScorer = "";
    int goalsForTeamA = 0;
    int goalsForTeamB = 0;
    int shotsForTeamA = 0;
    int shotsForTeamB = 0;
    int foulsForTeamA = 0;
    int foulsForTeamB = 0;
    int cornersForTeamA = 0;
    int cornersForTeamB = 0;
    int yellowCardsForTeamA = 0;
    int yellowCardsForTeamB = 0;
    int redCardsForTeamA = 0;
    int redCardsForTeamB = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // Displays scorers list
    public void displayScorers(String score) {
        TextView scoreView = (TextView) findViewById(R.id.listaMarcatori);
        scoreView.setText(String.valueOf(score));
    }


    /**
     * Displays score for Team A.
     */
    public void displayGoalsForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showGoalsForTeamA);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Displays score for Team B.
     */
    public void displayGoalsForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showGoalsForTeamB);
        scoreView.setText(String.valueOf(score));

    }

    /**
     * Displays shots for Team A.
     */
    public void displayShotsForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showShotsForTeamA);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Displays shots for Team B.
     */
    public void displayShotsForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showShotsForTeamB);
        scoreView.setText(String.valueOf(score));

    }

    /**
     * Displays corners for Team A.
     */
    public void displayCornersForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showCornersForTeamA);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Displays corners for Team B.
     */
    public void displayCornersForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showCornersForTeamB);
        scoreView.setText(String.valueOf(score));

    }

    /**
     * Displays fouls for Team A.
     */
    public void displayFoulsForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showFoulsForTeamA);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Displays fouls for Team B.
     */
    public void displayFoulsForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showFoulsForTeamB);
        scoreView.setText(String.valueOf(score));

    }

    /**
     * Displays yellow cards for Team A.
     */
    public void displayYellowCardsForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showYellowCardsForTeamA);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Displays yellow cards for Team B.
     */
    public void displayYellowCardsForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showYellowCardsForTeamB);
        scoreView.setText(String.valueOf(score));

    }

    /**
     * Displays red cards for Team A.
     */
    public void displayRedCardsForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showRedCardsForTeamA);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Displays red cards for Team B.
     */
    public void displayRedCardsForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.showRedCardsForTeamB);
        scoreView.setText(String.valueOf(score));

    }

    // Adds goal for team A
    public void addOneGoalForTeamA(View view) {

        goalsForTeamA = goalsForTeamA + 1;
        displayGoalsForTeamA(goalsForTeamA);

    }


    // Adds goal for team B
    public void addOneGoalForTeamB(View view) {

        goalsForTeamB = goalsForTeamB + 1;
        displayGoalsForTeamB(goalsForTeamB);
    }

    // Subtracts goal for team A and prevents stat provides negative number
    public void subtractOneGoalForTeamA(View view) {

        if (goalsForTeamA>0)

        {goalsForTeamA = goalsForTeamA - 1;
            displayGoalsForTeamA(goalsForTeamA);
        }
        else
        {goalsForTeamA = 0;
            displayGoalsForTeamA(goalsForTeamA);
        }
    }

    // Subtracts goal for team B and prevents stat provides negative number
    public void subtractOneGoalForTeamB(View view) {

        if (goalsForTeamB>0)

        {goalsForTeamB = goalsForTeamB - 1;
            displayGoalsForTeamB(goalsForTeamB);
        }
        else
        {goalsForTeamB = 0;
            displayGoalsForTeamB(goalsForTeamB);
        }
    }

    // Adds shot for team A
    public void addOneShotForTeamA(View view) {

        shotsForTeamA = shotsForTeamA + 1;
        displayShotsForTeamA(shotsForTeamA);
    }

    // Adds shot for team B
    public void addOneShotForTeamB(View view) {

        shotsForTeamB = shotsForTeamB + 1;
        displayShotsForTeamB(shotsForTeamB);
    }

    // Subtracts shot for team A and prevents stat provides negative number
    public void subtractOneShotForTeamA(View view) {

        if (shotsForTeamA>0)

        {shotsForTeamA = shotsForTeamA - 1;
        displayShotsForTeamA(shotsForTeamA);
        }
        else
        {shotsForTeamA = 0;
            displayShotsForTeamA(shotsForTeamA);
        }
    }

    // Subtracts shot for team B and prevents stat provides negative number
    public void subtractOneShotForTeamB(View view) {

        if (shotsForTeamB>0)

        {shotsForTeamB = shotsForTeamB - 1;
            displayShotsForTeamB(shotsForTeamB);
        }
        else
        {shotsForTeamB = 0;
            displayShotsForTeamB(shotsForTeamB);
        }
    }


    // Adds corner for team A
    public void addOneCornerForTeamA(View view) {

        cornersForTeamA = cornersForTeamA + 1;
        displayCornersForTeamA(cornersForTeamA);
    }

    // Adds corner for team B
    public void addOneCornerForTeamB(View view) {

        cornersForTeamB = cornersForTeamB + 1;
        displayCornersForTeamB(cornersForTeamB);
    }

    // Subtracts corner for team A and prevents stat provides negative number
    public void subtractOneCornerForTeamA(View view) {

        if (cornersForTeamA>0)

        {cornersForTeamA = cornersForTeamA - 1;
            displayCornersForTeamA(cornersForTeamA);
        }
        else
        {cornersForTeamA = 0;
            displayCornersForTeamA(cornersForTeamA);
        }
    }

    // Subtracts corner for team B and prevents stat provides negative number
    public void subtractOneCornerForTeamB(View view)  {

        if (cornersForTeamB>0)

        {cornersForTeamB = cornersForTeamB - 1;
            displayCornersForTeamB(cornersForTeamB);
        }
        else
        {cornersForTeamB = 0;
            displayCornersForTeamB(cornersForTeamB);
        }
    }


    // Adds foul for team A
    public void addOneFoulForTeamA(View view) {

        foulsForTeamA = foulsForTeamA + 1;
        displayFoulsForTeamA(foulsForTeamA);
    }

    // Adds foul for team B
    public void addOneFoulForTeamB(View view) {

        foulsForTeamB = foulsForTeamB + 1;
        displayFoulsForTeamB(foulsForTeamB);
    }

    // Subtracts foul for team A and prevents stat provides negative number
    public void subtractOneFoulForTeamA(View view)  {

        if (foulsForTeamA>0)

        {foulsForTeamA = foulsForTeamA - 1;
            displayFoulsForTeamA(foulsForTeamA);
        }
        else
        {foulsForTeamA = 0;
            displayFoulsForTeamA(foulsForTeamA);
        }
    }

    // Subtracts foul for team B and prevents stat provides negative number
    public void subtractOneFoulForTeamB(View view) {

        if (foulsForTeamB>0)

        {foulsForTeamB = foulsForTeamB - 1;
            displayFoulsForTeamB(foulsForTeamB);
        }
        else
        {foulsForTeamB = 0;
            displayFoulsForTeamB(foulsForTeamB);
        }
    }


    // Adds yellow card for team A
    public void addOneYellowCardForTeamA(View view) {

        yellowCardsForTeamA = yellowCardsForTeamA + 1;
        displayYellowCardsForTeamA(yellowCardsForTeamA);
    }

    // Adds yellow card for team B
    public void addOneYellowCardForTeamB(View view) {

        yellowCardsForTeamB = yellowCardsForTeamB + 1;
        displayYellowCardsForTeamB(yellowCardsForTeamB);
    }

    // Subtracts yellow card for team A and prevents stat provides negative number
    public void subtractOneYellowCardForTeamA(View view) {

        if (yellowCardsForTeamA>0)

        {yellowCardsForTeamA = yellowCardsForTeamA - 1;
            displayYellowCardsForTeamA(yellowCardsForTeamA);
        }
        else
        {yellowCardsForTeamA = 0;
            displayYellowCardsForTeamA(yellowCardsForTeamA);
        }
    }

    // Subtracts yellow card for team B and prevents stat provides negative number
    public void subtractOneYellowCardForTeamB(View view) {

        if (yellowCardsForTeamB>0)

        {yellowCardsForTeamB = yellowCardsForTeamB - 1;
            displayYellowCardsForTeamB(yellowCardsForTeamB);
        }
        else
        {yellowCardsForTeamB = 0;
            displayYellowCardsForTeamB(yellowCardsForTeamB);
        }
    }


    // Adds red card for team A
    public void addOneRedCardForTeamA(View view) {

        redCardsForTeamA = redCardsForTeamA + 1;
        displayRedCardsForTeamA(redCardsForTeamA);
    }

    // Adds red card for team B
    public void addOneRedCardForTeamB(View view) {

        redCardsForTeamB = redCardsForTeamB + 1;
        displayRedCardsForTeamB(redCardsForTeamB);
    }

    // Subtracts red card for team A and prevents stat provides negative number
    public void subtractOneRedCardForTeamA(View view) {

        if (redCardsForTeamA>0)

        {redCardsForTeamA = redCardsForTeamA - 1;
            displayRedCardsForTeamA(redCardsForTeamA);
        }
        else
        {redCardsForTeamA = 0;
            displayRedCardsForTeamA(redCardsForTeamA);
        }
    }

    // Subtracts red card for team B and prevents stat provides negative number
    public void subtractOneRedCardForTeamB(View view) {

        if (redCardsForTeamB>0)

        {redCardsForTeamB = redCardsForTeamB - 1;
            displayRedCardsForTeamB(redCardsForTeamB);
        }
        else
        {redCardsForTeamB = 0;
            displayRedCardsForTeamB(redCardsForTeamB);
        }
    }

// Resets match stats

    public void resetMatchStats(View view) {
        scorerNames = "Marcatori: ";
        displayScorers(scorerNames);
        lastScorer = "";
        goalsForTeamA = 0;
        displayGoalsForTeamA(goalsForTeamA);
        goalsForTeamB = 0;
        displayGoalsForTeamB(goalsForTeamB);
        shotsForTeamA = 0;
        displayShotsForTeamA(shotsForTeamA);
        shotsForTeamB = 0;
        displayShotsForTeamB(shotsForTeamB);
        foulsForTeamA = 0;
        displayFoulsForTeamA(foulsForTeamA);
        foulsForTeamB = 0;
        displayFoulsForTeamB(foulsForTeamB);
        cornersForTeamA = 0;
        displayCornersForTeamA(cornersForTeamA);
        cornersForTeamB = 0;
        displayCornersForTeamB(cornersForTeamB);
        yellowCardsForTeamA = 0;
        displayYellowCardsForTeamA(yellowCardsForTeamA);
        yellowCardsForTeamB = 0;
        displayYellowCardsForTeamB(yellowCardsForTeamB);
        redCardsForTeamA = 0;
        displayRedCardsForTeamA(redCardsForTeamA);
        redCardsForTeamB = 0;
        displayRedCardsForTeamB(redCardsForTeamB);
    }



    //Adds last scorer in editText to scorers list and displays the updated scorers list

    //If the add scorer field is empty, it doesn't update variable lastScorer.
    //Else it takes the value from add scorer field, assign it to variable lastScorer, then it  displays updated scorers
    public void addScorer(View view) {
        EditText addScorer = (EditText) findViewById(R.id.addScorerField);
        String lastScorerPrep = addScorer.getText().toString();
        if (StringUtils.isEmpty(lastScorerPrep))
        {

        }
        else {
            lastScorer = lastScorerPrep;
            scorerNames = scorerNames + lastScorer + ", ";
            displayScorers(scorerNames);
        }
    }

    //Deletes last scorer from scorers list
    public void deleteLastScorer(View view) {

        scorerNames = scorerNames.replaceFirst((lastScorer + ", "), "");
        displayScorers(scorerNames);
    }
}

你必须在html标签中加入想要的弹出窗口。并在谷歌地图标签中的代码初始化它。 并使用 <div class="map-popupstring hidden" id="popupstring1"> <div class="text-center"> <h3>title</h3> <p>subinfo</p> </div> </div> 标记在点击标记时初始化它。

data-popupstring-id="#popupstring1"

答案 1 :(得分:0)

我通过添加此代码解决了这个问题:

的代码:

 var popup = new google.maps.InfoWindow({
                    content: feature,
                    maxWidth: 300
                });

          google.maps.event.addListener(marker, "click", function() {
                    if (currentPopup != null) {
                        currentPopup.close();
                        currentPopup = null;
                    }
                    popup.open(map, marker);
                    currentPopup = popup;
                });
                google.maps.event.addListener(popup, "closeclick", function() {
                    map.panTo(center);
                    currentPopup = null;
                });