2017-11-08 5 views
-1

Android 기기에 GoogleMap을 가져올 수 있는데 문제가 내 currentlocation을 가리키고 있지 않습니다.내 마커가 현재 위치를 가리키지 않는 이유

:

아래
$ ionic cordova plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE" 

$ npm install --save @ionic-native/google-maps 

내 구글지도 내 안드로이드 장치에

아래

enter image description here

내 코드되는 모습입니다 :

나는이 두 패키지를 설치 한

ionViewDidLoad() { 
    console.log('ionViewDidLoad GoogleMapTestPage'); 
    this.loadMap(); 
    } 


ngAfterViewInit() { 
//this.loadMap();   
} 

loadMap() { 

// create a new map by passing HTMLElement 
let element: HTMLElement = document.getElementById('map'); 

let map: GoogleMap = this.googleMaps.create(element); 

// listen to MAP_READY event 
// You must wait for this event to fire before adding something to the map or modifying it in anyway 
map.one(GoogleMapsEvent.MAP_READY).then(
    () => { 
    console.log('Map is ready!'); 
    // Now you can add elements to the map like the marker 
    } 
); 

// create LatLng object 
let ionic: LatLng = new LatLng(43.0741904,-89.3809802); 

// create CameraPosition 
let position: any = { 
    target: ionic, 
    zoom: 18, 
    tilt: 30 
}; 

// move the map's camera to position 
map.moveCamera(position); 

// create new marker 
let markerOptions: MarkerOptions = { 
    position: ionic, 
    title: 'Ionic' 
}; 

const marker:any = map.addMarker(markerOptions) 
    .then((marker: Marker) => { 
     marker.showInfoWindow(); 
    }); 
} 

내 HTML 코드

<ion-content>  
     <div #map id="map" style="height:100%;"></div> 
</ion-content> 

내 질문 :

어떻게 위의 코드 내 current location를 가리 키도록!

내가 3 이온에 새로운 오전 나를 도와주세요, 그냥 구글에

위치에 현재 사용자를 가리 키도록 싶어!

답변

0

마커를 추가하려면지도 객체에 마커를 추가해야합니다 (& lng 및 제목). 살펴보기 :

map.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Title")); 

해피 코딩 !!

+0

곳이 코드를 추가하려면? 그것은 Google지도에서 현재 위치를 제공 할 것입니다. –

+0

또한 현재 위치에 대한 포인터를 얻고 싶습니다. 현재 위치는 –

+0

이며 위치 정보에서 위도와 경도를 구하고 위 코드로 설정해야합니다.위 라인을 추가하기 위해 코드에서 맵 객체를 만들었습니다. –

2

준비가 완료되면 마커 을 추가하는 것과 같이지도를 변경해야합니다.. 마커 설정 코드를 map.one(GoogleMapsEvent.MAP_READY)으로 이동하십시오.

let ionic: LatLng = new LatLng(43.0741904,-89.3809802); 

// create CameraPosition 
let position: any = { 
    target: ionic, 
    zoom: 18, 
    tilt: 30 
}; 

map.one(GoogleMapsEvent.MAP_READY).then(
    () => { 
    console.log('Map is ready!'); 
    // Now you can add elements to the map like the marker 
     map.moveCamera(position); 

    // create new marker 
    let markerOptions: MarkerOptions = { 
     position: ionic, 
     title: 'Ionic' 
    }; 

    const marker:any = map.addMarker(markerOptions) 
     .then((marker: Marker) => { 
      marker.showInfoWindow(); 
     }); 
    } 
    } 
); 
-1

첫째 있는지 당신의 API 키가 유효하고이 점을 추가 매니페스트`` 는 그 다음 onMapReady (GoogleMap으로지도) 방법 내부 (참) map.setMyLocationEnabled을 추가 사용합니다. 이

@Override

public void onMapReady(GoogleMap gMap) { 
    mGoogleMap = gMap; 
    mGoogleMap.setMyLocationEnabled(true); 
    buildGoogleApiClient(); 
    mGoogleApiClient.connect(); 

} 
+0

여기가 기본 Android라고 가정합니다. OP는 하이브리드 인 ionic 태그를 가지고 있습니다 ... –

1

처럼이 시도하고 수업 후 생성자 전에 변수 map:GoogleMap;을 선언합니다. 따라서지도를 렌더링하지 않고지도를 재사용 할 수 있습니다. 모든 단일로드 페이지로 인해 Google지도 API 할당량이 증가하고 있습니다.

나의 제안 : 공급자에 넣어로드 맵

this.map.one(GoogleMapsEvent.MAP_READY).then(()=>{ 
 
     console.log("Map ready"); 
 
     this.map.setMyLocationEnabled(true); 
 
     this.map.getMyLocation({enableHighAccuracy:true}).then(pos=>{ 
 
     this.map.setCameraTarget(pos.latLng); 
 
     this.map.animateCamera({ 
 
      target:pos.latLng 
 
     }); 
 

 
     this.map.addMarker({ 
 
      title: 'You', 
 
      icon: 'pin' 
 
      animation: 'DROP', 
 
      position: pos.latLng 
 
     }).then((marker)=>{ 
 
      this.userMarker = marker; 
 
      marker.setPosition(pos.latLng); 
 

 
      }); 
 
     }); 
 
     });

+0

Reza, cordova-plugin-googlemaps (@ ionic-native/googlemaps)는 JavaScript API가 아닌 기본 Google Maps API를 사용합니다. Google은 기본 Google지도 API에 대한 할당량을 설정하지 않습니다. – wf9a5m75

+0

@ wf9a5m75 와우! Google지도 할당량을 사용하여 요금이 부과되지 않는다는 것을 알 때 매우 좋습니다. D – Reza