集成Google Maps以获取angular 2中的“当前位置"吗? [英] Integrating Google Maps to get Current Location in angular 2?

查看:56
本文介绍了集成Google Maps以获取angular 2中的“当前位置"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Angle 2中使用Google Maps API获取当前位置?我已经在我的应用程序中包含了Google地图.现在,我想捕获当前位置,并附加了我的代码.请帮助我

How do i use google maps API in angular 2 to get Current location ? i've included the google map in my application. now i want to capture current location i've attached my code . Please Help me

import { Component,OnInit,ElementRef,ViewChild} from '@angular/core';

// import {searchComponent} from './getLocation.Component';
declare var google: any;

@Component({
    selector: 'map',
    moduleId:module.id,
    templateUrl:'geoCodeMap.component.html',
})

export class mapComponent{
    searchBox:any;
    map:any;
    marker:any;
    accuracy:any;
    @ViewChild('mapDiv') mapDiv:ElementRef;
    options={
       center:{
                lat : 17.555,
                lng : 78.555
            },
    zoom:15,
    mapTypeControl:false,
    MapTypeId: google.maps.MapTypeId.TERRAIN,

    };


    ngAfterViewInit(){
        this.map=new google.maps.Map(this.mapDiv.nativeElement,this.options); 
        this.getLocation();
        this.onSearchResultSelect();
    }      
    getLocation() 
    {
        if (navigator.geolocation) {
            var self = this;
            navigator.geolocation.getCurrentPosition(function(response){
                self.showPosition(response, self);
            }, function() {
            alert("Unable to get GPS Location");
            }, {
            enableHighAccuracy : true
            });
        }
        else {
        alert("Geolocation is not supported by this browser.");
        }
    }



    showPosition(position:any, self:any) {
        var icon = {
        url: "images/home.png",
        scaledSize: new google.maps.Size(30, 30), // scaled size
        origin: new google.maps.Point(0,0), // origin
        anchor: new google.maps.Point(0, 0)
    };
    var myLatLng = new google.maps.LatLng(position.coords.latitude,
            position.coords.longitude);
    self.marker = new google.maps.Marker({
        position : myLatLng,
        map:self.map,
        icon:icon,
        draggable : true,
        title : 'Mark Home'
    });

    self.map.panTo(myLatLng);
    self.accuracy = position.coords.accuracy;

    google.maps.event.addListener(self.map, 'dragstart', function() {
    });  
    google.maps.event.addListener(self.map, 'dragend', function() {
    });


    google.maps.event.addListener(self.map, 'center_changed', function() {
    });



    }

 onSearchResultSelect() {
             var self=this;
         var searchMarker:any;
    var input = document.getElementById('pac-input');
    this.searchBox = new google.maps.places.SearchBox(input);
    var button = document.getElementById('button');
    button.onclick = function() {
    input.value='';
    input.focus();
        var places = this.searchBox.getPlaces();

        if (places.length == 0) {
            return;
        }
        if (searchMarker != null) {
            searchMarker.setMap(null);
        }

        var empGeocode = self.map.getCenter().lat() + "," + self.map.getCenter().lng();
        var place = places[0];

        self.map.setCenter(place.geometry.location);
        //container.html(html);
     }
    }

}

当我检查浏览器的控制台时,得到"map"未定义的错误:(-已解决.

I get an error as 'map' undefined when i check the browser's console :( -- Solved .

我现在要搜索特定位置.我已经提到了Google Maps API文档.但是我没有结果.任何想法都必须更改我的代码??

I now want to search for a particular location . I've referred to Google Maps API docs . But i got no results.Any idea how my code has to be changed ??

推荐答案

考虑使用google.maps.places进行搜索.

在此处查看更多信息: https://developers.google.com/maps/documentation/javascript/places

See more here: https://developers.google.com/maps/documentation/javascript/places

这篇关于集成Google Maps以获取angular 2中的“当前位置"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆