简体   繁体   English

Google Maps:无法更改标记图标

[英]Google Maps: Can't Change Marker Icon

I have created a method to find user location using Geolocation and i pass that location to my function to make a request to a web service. 我创建了一种使用Geolocation查找用户位置的方法,并将该位置传递给函数以向Web服务发出请求。 After data from the web service received, some marker will be added to the map. 从Web服务接收到数据后,一些标记将添加到地图中。 The marker was shown to the map, but apparently when i try to change the marker icon, the marker was dissapear. 该标记已显示在地图上,但是显然当我尝试更改标记图标时,该标记消失了。 i've tried to put it in the same folder and the image name is correct and i've tried to open the image in my browser and it open. 我尝试将其放在同一文件夹中,并且图像名称正确,并且尝试在浏览器中打开图像并将其打开。 Here is my code, can anyone please help me? 这是我的代码,有人可以帮我吗?

    if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        initialLocation = new google.maps.LatLng(lat,lon);
        map.setCenter(initialLocation);
        get_json(lat,lon,function(data){
            var i, marker;
            var location;
            for (i=0; i<=data.cells.length;i++) {
                location = new google.maps.LatLng(data.cells[i].lat, data.cells);
                marker = new google.maps.Marker({
                    position: location,
                    map: map,
                    icon: "../img/icon_bts.png"
                });    
            }
        });
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  }

Seems like you accidentally deleted part of your script when you added your own marker. 似乎您在添加自己的标记时不小心删除了脚本的一部分。 Assuming you posted your literal code fragment (and if you didn't, you really should ), the following line 假设您张贴了您的文字代码片段(如果没有, 您确实应该这样做 ),则以下行

location = new google.maps.LatLng(data.cells[i].lat, data.cells);

could never work . 无法工作 The constructor of LatLng takes two number arguments: if data.cells[i].lat is a number, data.cells definitely isn't. LatLng构造函数采用两个数字参数:如果data.cells[i].lat是数字,则data.cells绝对不是。 I assume that line should read something like 我认为那行应该读类似

location = new google.maps.LatLng(data.cells[i].lat, data.cells[i].lng);

If that doesn't solve your problem, just look in the browser console what the actual error is and report it as part of your question. 如果那不能解决您的问题,只需在浏览器控制台中查看实际错误是什么,并将其报告为问题的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM