更改谷歌地图标记位置onclick

时间:2018-04-07 04:12:23

标签: javascript google-maps

当我点击地图时,我想移动我的标记并获得long和lat。 目前,我的地图已显示,但我无法更改标记位置。 我试着在this中模仿编码器。

我的参考使用按钮更改标记的位置。在我的情况下,我需要点击地图。我试图修改它以满足我的情况但仍然无法正常工作。

这是我的代码:

import java.util.Scanner;

public class Solution1 {
    public static void main(final String[] args) {
        final Scanner scan = new Scanner(System.in);
        final int t = scan.nextInt(); // number of test cases
        for (int i = 0; i < t; ++i) {
            final long maximumDamage = scan.nextLong();
            final char[] a = scan.next().toCharArray();

            long minDamage = 0; for (final char c : a) if ('S' == c) minDamage++;

            if (maximumDamage >= minDamage) {
                long hacks = 0;
                while (damageComputer(a) > maximumDamage) {
                    int swappos;
                    while ((swappos = swappos(a)) >= 0) {
                        a[swappos] = 'S'; a[swappos + 1] = 'C'; hacks++;
                    }
                }
                System.out.println("Case #" + i + ": " + hacks);
            } else
                System.out.println("Case #" + i + ": " + "IMPOSSIBLE");
        }
    }
    public static long damageComputer(final char[] a) {
        long strength = 1, damage = 0;
        for (final char c : a) if ('C' == c) strength <<= 1; else damage += strength;
        return damage;
    }
    public static int swappos(final char[] a) {
        for (int i = a.length - 2; i > 0; --i)
            if ((a[i] == 'C') && (a[i + 1] == 'S')) return i;
        return -1;
    }
}

1 个答案:

答案 0 :(得分:1)

1)点击地图并将标记的位置更改为您点击的位置

var map, marker;
$(document).ready(function() {

    function myMap(lat,long) {
        var myCenter = new google.maps.LatLng(lat,long);
        var mapCanvas = document.getElementById("googleMap");

        var mapOptions = {
            center: myCenter, 
            zoom: 15,
            treetViewControl: false,
            mapTypeControl: false
        };

        map = new google.maps.Map(mapCanvas, mapOptions);
        marker = new google.maps.Marker(
            {
                position:myCenter,
                draggable: true
            }
        );
        marker.setMap(map);

        // Zoom to 9 when clicking on marker
        google.maps.event.addListener(marker,'click',function() {
            map.setZoom(9);
            map.setCenter(marker.getPosition());
        });

        //sets variable for lat and long
        $('.lat').text(lat);
        $('.long').text(long);
    }

    function newLocation(newLat,newLng)
    {
        map.setCenter({
            lat : newLat,
            lng : newLng
        });
    }

    google.maps.event.addDomListener(window, 'load', myMap(3.1412,101.68653));


    $(document).ready(function ()
    {
        // click on map and set you marker to that position
        google.maps.event.addListener(map, 'click', function(event) {
            marker.setPosition(event.latLng);
        });
    });

});

2)通过拖放来改变标记的位置

var map;
$(document).ready(function() {

    function myMap(lat,long) {
        var myCenter = new google.maps.LatLng(lat,long);
        var mapCanvas = document.getElementById("googleMap");

        var mapOptions = {
            center: myCenter, 
            zoom: 15,
            treetViewControl: false,
            mapTypeControl: false
        };

        map = new google.maps.Map(mapCanvas, mapOptions);
        var marker = new google.maps.Marker(
            {
                position:myCenter,
                draggable: true // set marker draggable
            }
        );
        marker.setMap(map);

        // Zoom to 9 when clicking on marker
        google.maps.event.addListener(marker,'click',function() {
            map.setZoom(9);
            map.setCenter(marker.getPosition());
        });

        // when dragend, show new lat and lng in console
        google.maps.event.addListener(marker, 'dragend', function(){
            console.log("lat: "+marker.position.lat())
            console.log("lng: "+marker.position.lng())
        })

        //sets variable for lat and long
        $('.lat').text(lat);
        $('.long').text(long);
    }

    function newLocation(newLat,newLng)
    {
        map.setCenter({
            lat : newLat,
            lng : newLng
        });
    }

    google.maps.event.addDomListener(window, 'load', myMap(3.1412,101.68653));


    $(document).ready(function ()
    {
        google.maps.event.addListener(map, 'click', function(event) {
            newLocation(event.myCenter);
        });
    });

});