Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我的标记没有指向我的当前位置_Android_Google Maps_Cordova_Ionic3_Ionic Native - Fatal编程技术网

Android 为什么我的标记没有指向我的当前位置

Android 为什么我的标记没有指向我的当前位置,android,google-maps,cordova,ionic3,ionic-native,Android,Google Maps,Cordova,Ionic3,Ionic Native,我可以在我的Android设备上获取GoogleMap,问题是它没有指向我的当前位置 我已安装以下两个软件包: $ 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 @i

我可以在我的
Android
设备上获取
GoogleMap
,问题是它没有指向我的
当前位置

我已安装以下两个软件包:

$ 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
下面是我的谷歌地图在android设备上的外观

下面是我的代码:

  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>

我的问题:

如何在上述代码中指向我的
当前位置

请帮助我,因为我是离子3的新手,只是我想指出当前的用户


谷歌上的位置

对于添加标记,您必须在地图对象上添加带有lat&lng和标题的标记。 看看:

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

快乐编码

您需要在地图上进行更改,如在地图准备就绪后添加标记。因此,将标记设置代码移动到
map.one(GoogleMapsEvent.map\u READY)


首先,确保您的API密钥有效,并将其添加到清单中`` 然后在onMapReady(GoogleMap)方法中使用add map.setMyLocationEnabled(true)。像这样

@凌驾

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

}

试试这个并声明变量
map:GoogleMap在类后的构造函数之前。所以你可以在不渲染地图的情况下重用你的地图。因为每一个加载页面你的谷歌地图api配额都在增加

我的建议是: 将加载映射放在提供程序上
this.map.one(GoogleMapsEvent.map_READY)。然后(()=>{
控制台日志(“地图就绪”);
this.map.setMyLocationEnabled(true);
this.map.getMyLocation({enableHighAccurance:true})。然后(pos=>{
此.map.setCameraTarget(位置latLng);
这是一张地图({
目标:latLng位置
});
这个是.map.addMarker({
标题:"你",,
图标:“pin”
动画:“放下”,
位置:pos.latLng
}).然后((标记)=>{
this.userMarker=marker;
标记器设置位置(位置板条);
});
});

});在哪里添加此代码?它会给出谷歌地图中的当前位置。对于当前位置,您必须从geolocation获取lat和lng,并将其设置为上述代码。为了添加上面的行,您在代码中创建了map对象。有关更多信息,请查看我假设这是本机android?OP已经标记了ionic,它是混合的…Reza,cordova插件googlemaps(@ionic native/googlemaps)使用本机googlemaps API,而不是JavaScript API。谷歌没有为本机谷歌地图API设置配额。@wf9a5m75哇!当我知道我们不用使用谷歌地图来收费时,这真是太棒了
public void onMapReady(GoogleMap gMap) {
   mGoogleMap = gMap;
   mGoogleMap.setMyLocationEnabled(true);
   buildGoogleApiClient();
   mGoogleApiClient.connect();

}