我正在尝试在浏览器中显示来自其他位置的路线currentlocation
,但是当我在电话中尝试操作时似乎不起作用。
我的意思是中心地图不起作用,路线不显示
对不起,我的英语不好:(
ngOnInit() {
this.loadMap();
}
loadMap() {
try {
this.map = new google.maps.Map(document.getElementById("map"), {
zoom: 7,
center: { lat: 41.85, lng: -87.65 }
});
} catch (e) {
window.alert("No CurrentLocation " + e);
}
this.directionsRenderer.setMap(this.map);
}
//button for currentlocation
getPosition(): any {
this.geolocation.getCurrentPosition().then(resp => {
this.setCenter(resp);
});
}
setCenter(position: Geoposition) {
this.myLatLng = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
this.map.setCenter(this.myLatLng);
}
calculateAndDisplayRoute(destination): void {
var t = this;
this.directionsService.route(
{
origin: t.myLatLng,
destination: t.destination.text,
travelMode: "DRIVING"
},
(response, status) => {
if (status === "OK") {
this.directionsRenderer.setDirections(response);
} else {
window.alert("Directions request failed due to " + status);
}
}
);
}
答案 0 :(得分:0)
您的问题与地图无关,而与Geolocation plugin有关。
PositionError
是defined like this:
PositionError.prototype.PERMISSION_DENIED = PositionError.PERMISSION_DENIED = 1;
PositionError.prototype.POSITION_UNAVAILABLE = PositionError.POSITION_UNAVAILABLE = 2;
PositionError.prototype.TIMEOUT = PositionError.TIMEOUT = 3;
所以您遇到了这三个问题之一。
您还没有说您要在哪个平台上进行此尝试。对于iOS,您必须将此配置添加到您的configuration.xml文件中:
<edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
<string>We use your location for full functionality of certain app features.</string>
</edit-config>
此代码也不起作用:
getPosition(): any {
this.geolocation.getCurrentPosition().then(resp => {
this.setCenter(resp);
});
}
有一个示例in the documentation,显示了返回的内容。如果不确定,则应将内容记录到控制台进行调查。
let lat = resp.coords.latitude;
let lon = resp.coords.longitude;
您的setCenter()
也不正确,it expects a google maps LatLng or LatLngLiteral object如下:
this.setCenter({lat: -34, lng: 151});
这应该类似于:
getPosition(): any {
this.geolocation.getCurrentPosition().then(resp => {
let lat = resp.coords.latitude;
let lng = resp.coords.longitude;
this.setCenter({lat, lng});
}).catch((error) => {
console.log('Error getting location', error);
});
}
编程是一件精确的事情。您必须查找您正在做的所有事情,并确保它们符合规范,否则您将无所适从。