【问题标题】:How can i use custom icon in ng-map marker(angular)?如何在 ng-map 标记(角度)中使用自定义图标?
【发布时间】:2016-07-03 09:01:58
【问题描述】:

我有包含经度和纬度的商店列表,并且我有特定的用户纬度和经度。

当用户点击地图按钮时,地图中会显示两个标记,分别是用户和商店位置

如何更改这两个标记图标而不是默认标记 A 和 B 如何使用我自己的自定义图标作为标记 1.http://www.clker.com/cliparts/U/M/C/p/x/C/google-maps-pin-green.svg

2.http://www.clker.com/cliparts/q/I/Q/u/Z/1/marker-hi.png

angular.module('myApp', ['ngMap'])
    .controller("myCntrl", function ($scope) {
      var _lat1 =12.904778 ;
	    var _lon1 =77.585680;
    	$scope.lat = _lat1;
    	$scope.lon = _lon1;
      $scope.positions = [{pos:[$scope.lat, $scope.lon],name:"User"}];
      $scope.center = [$scope.lat, $scope.lon];
      
      
      $scope.viewMap = false;
      $scope.updateMap = function(dealer) {
        $scope.lat1 = dealer.S_Latitude;
    	  $scope.lon1 = dealer.S_Longitude;
    	  icon : 'http://www.clker.com/cliparts/q/I/Q/u/Z/1/marker-hi.png',
        $scope.positions = [{pos:[$scope.lat, $scope.lon],name:"User"}, {pos:[dealer.S_Latitude, dealer.S_Longitude],name:"Store"}];
        $scope.viewMap = true;
        $scope.path = [[$scope.lat, $scope.lon], [dealer.S_Latitude, dealer.S_Longitude]];
      }
    
    $scope.dealers = [{
        
        S_Email_id: "aditiya@gmail.com",
        S_Store: "samsung",
        Store_Name: "Adtiya Samsung Store",
        S_Services: "Regular Service,Software Faults,Hardware Faults",
        Store_long_description: "Undertake all kind of samsung mobiles",
        Store_short_description: "Undertake all kind of samsung mobiles",
		S_Latitude: "12.93489905",
		S_Longitude: "77.57070772",
		S_clocation: ""
    }, {
       
        S_Email_id: "rajs@gmail.com",
        S_Store: "nokia",
        Store_Name: "sri shakthi mobile service",
        S_Services: "Settings Faults,Regular Service,Hardware Faults",
        Store_long_description: "Undertake all kind of nokia mobiles",
        Store_short_description: "Undertake all kind of nokia mobiles",
		S_Latitude: "12.9599264",
		S_Longitude: "77.5924983",
		S_clocation: ""
    }, {
        
        S_Email_id: "sprtive23@gmail.com",
        S_Store: "nokia,samsung",
        Store_Name: "sun mobile service center",
        S_Services: "Regular Service,overall maintenance,Mobile Shield Installation",
        Store_long_description: "Undertake all kind of nokia,samsung mobiles",
        Store_short_description: "Undertake all kind of nokia,samsung mobiles",
		S_Latitude: "12.911229",
		S_Longitude: "77.519281",
		S_clocation:""
		},
	{
        
        S_Email_id: "super@gmail.com",
        S_Store: "nokia,samsung",
        Store_Name: "ragu mobile service center",
        S_Services: "Regular Service,overall maintenance,Mobile Shield Installation",
        Store_long_description: "Undertake all kind of nokia,samsung mobiles",
        Store_short_description: "Undertake all kind of nokia,samsung mobiles",
		S_Latitude: "12.909999",
		S_Longitude: "77.506871",
		S_clocation: ""
		}
    ]

 
  
  

	}
)
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script src="http://maps.google.com/maps/api/js?libraries=placeses,visualization,drawing,geometry,places"></script>
<script src="http://code.angularjs.org/1.4.7/angular.js"></script>
<script src="http://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>

<style>
    

    .custom-marker {
    font-size: 2em;
    padding: 10px;
    background: #fff;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    border: #7F7F7F solid 1px;
    text-align: center;
    }
    .custom-marker:after {
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 7px 6px 0;
    border-color: #fff transparent;
    display: block;
    width: 0;
    z-index: 1;
    margin-left: -6px;
    bottom: -6px;
    left: 50%;
    }
    .custom-marker:before {
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 7px 6px 0;
    border-color: #7F7F7F transparent;
    display: block;
    width: 0;
    z-index: 0;
    margin-left: -6px;
    bottom: -7px;
    left: 50%;
    }
    </style>
</head>

<body>
  <div ng-controller="myCntrl">
      <label>Case sensitive Search on Label</label><br>
      <input ng-model="query" type="text" placeholder="Search for name" />
      <div>
        <ng-map zoom="12" center="{{lat}}, {{lon}}">
          
          <directions
          draggable="true"
          panel="directions-panel"
          travel-mode="DRIVING"
          origin="{{lat}}, {{lon}}"
          destination="{{lat1}}, {{lon1}}">
        </directions>
        
          
        </ng-map>
        <br><br><br>
      </div>
      <div>
      <div ng-repeat="dealer in dealers">
        {{dealer.Store_Name}}<br>
				{{dealer.S_Email_id}}<br>
				{{dealer.S_clocation}}
        
        <input type="button" name="map" id="map" value="map" ng-click="updateMap(dealer);"><br><br>
			</div>
			</div>
			<br><br><br>
  </div>
</body>
</html>

Demo

【问题讨论】:

    标签: javascript angularjs google-maps ng-map


    【解决方案1】:

    不要使用自定义标记指令。对于任何大量标记(例如 200+),它都会扼杀你的表现。而是使用标准标记的“图标”属性,如下所示:

    <marker id="L: {{loc.position[0]}}, {{loc.position[1]}}" position="{{loc.position}}" icon="{ url:'/assets/images/deal-indicator.png', scaledSize:[32,40], origin: [0,0], anchor: [16,40] }">
    

    marker 指令只是 Google Maps API v3 标准“marker”的封装,因此使用 ng-map 时可以访问所有标准标记属性。

    图标是一个Javascript对象,属性如下(来自文档):

    属性

    锚点 |类型:点

    锚定图像的位置,对应于 地图上标记的位置。默认情况下,锚点位于 沿着图像底部的中心点。

    原产地 |类型:点

    图像在精灵中的位置,如果有的话。默认情况下, 原点位于图像的左上角 (0, 0)。

    也可以通过使用标记的“setIcon”方法以编程方式设置图标,例如:

    myMarker.setIcon('/assets/images/deal-indicator-selected.png');
    

    或者,先构造一个图标对象并将其设置为标记:

    var customIcon = {
        url: "/images/my-marker.png",
        scaledSize: new google.maps.Size(32, 40),
        origin: new google.maps.Point(0, 0), 
        anchor: new google.maps.Point(16, 40)
    };
    
    myMarker.setIcon(customIcon);
    

    【讨论】:

      【解决方案2】:

      请阅读有关ng-maps 的自定义标记的文档: https://ngmap.github.io/#/!custom-marker.html

      <custom-marker id="usa" position="united state of america" on-click="ifNeeded()"></custom-marker>
      

      编辑:下面的完整代码;

      <ng-map zoom="12" center="{{lat}}, {{lon}}">
      
            <directions
            draggable="true"
            panel="directions-panel"
            travel-mode="DRIVING"
            origin="{{lat}}, {{lon}}"
            destination="{{lat1}}, {{lon1}}">
          </directions>
      
          <custom-marker id="can" position="canada" on-click="click()">
              <div>
                  <b>Hi, USA</b>
                  <img src="http://icons.iconarchive.com/icons/custom-icon-design/2014-world-cup-flags/32/USA-icon.png" />
              </div>
          </custom-marker>
      
      </ng-map>
      

      【讨论】:

      • 我是 Angular 新手,请帮帮我。如果您不介意请更新我的代码
      • 我已经为你提供了答案,我不会为你写代码,这不是 StackOverflow 的用途。
      • Acutally 我不知道我必须在哪里添加该代码。我很挣扎这就是为什么我问对不起
      • 没有我的期望。当用户单击地图按钮时,现在显示两个标记图标。而不是两个标记图标,我们如何在地图中显示两个不同的标记自定义标记图标
      • 没有工作可以请你检查一下。plnkr.co/edit/6ZY5YdaR5qrdw41jpO2t?p=preview
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2021-08-08
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2018-03-17
      相关资源
      最近更新 更多