我想使用Angularjs在Google地图中设置所需的位置

时间:2014-12-10 06:10:53

标签: angularjs ionic-framework

我在离子框架中添加了Google地图,它可以访问我的位置。 我想在Google地图中添加其他位置,请提供一些说明。

这是我的控制器

.controller('MapCtrl',function($ scope,$ ionicLoading,$ compile){

$scope.init = function() {
    var myLatlng = new google.maps.LatLng(43.07493,-89.381388);

    var mapOptions = {
      center: myLatlng,
      zoom: 16,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"),
        mapOptions);


    var contentString = "<div><a ng-click='clickTest()'>Click me!</a></div>";
    var compiled = $compile(contentString)($scope);

    var infowindow = new google.maps.InfoWindow({
      content: compiled[0]
    });

    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)'
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });

    $scope.map = map;





};

// google.maps.event.addDomListener(window, 'load', initialize);

$scope.centerOnMe = function() {
    if(!$scope.map) {
        return;
    }

    $scope.loading = $ionicLoading.show({
      content: 'Getting current location...',
      showBackdrop: false
    });

    navigator.geolocation.getCurrentPosition(function(pos) {
      $scope.map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
      var p1 =new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
      console.log(p1);
      $ionicLoading.hide();
    }, function(error) {
      alert('Unable to get location: ' + error.message);
    });
};





$scope.clickTest = function() {
    alert('Example of infowindow with ng-click')
};

})

1 个答案:

答案 0 :(得分:0)

试试这个

navigator.geolocation.getCurrentPosition(function(pos) {
      $scope.map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
      var p1 =new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
      console.log(p1);
      //set up the marker for my location
      var mylocationMarker = new google.maps.Marker({
      position:p1,
      map: map,
      title: 'My location'
    });
    }, function(error) {
      alert('Unable to get location: ' + error.message);
    });