簡體   English   中英

Google Maps API NativeScript 自定義標記圖標

[英]Google Maps API NativeScript custom marker icon

我嘗試使用 NativeScript 和 Google 地圖 API 包創建一個帶有自定義圖標的標記。 我將我的圖標添加到 android 資源中,然后構建了我的項目幾次。

   var marker = new mapsModule.Marker();
   marker.position = mapsModule.Position.positionFromLatLng(result.latitude, result.longitude);
   marker.title = "Home";
   var icon = new Image();
   icon.imageSource = imageSource.fromResource('bot_marker');
   marker.icon = icon;
   marker.snippet = "This is where I live";
   marker.userData = { index: 1 };
   mapView.addMarker(marker);

即使我嘗試使用資源的iconlogo ,它也不會出現。

使用 Angular2

import { Image } from "ui/image";
import { ImageSource } from "image-source";

    onMapReady(event) {
        console.log("Google map is ready!");

        var mapView = event.object;
        var marker = new Marker();
        marker.position = Position.positionFromLatLng(this.state.latitude, this.state.longitude);

        let imgSrc = new ImageSource();
        imgSrc.fromResource("pink_pin_dot");

        let image = new Image();
        image.imageSource = imgSrc;

        marker.icon = image;
        mapView.addMarker(marker);
    }

使用 Vue 這對我有用。

注 1: fromResourcefromFile已被棄用,新函數被稱為fromFileSyncfromResourceSync

注意 2 :設置圖標后不要更改標記顏色,這將向您顯示默認圖標,盡管您已經為自定義圖標正確設置了 ImageSource

1 ) 導入所需的 NativeScript 庫

import { Image } from "ui/image";
import { ImageSource } from "image-source";
import { Marker } from "nativescript-google-maps-sdk";

2 a) 使用本地路徑添加自定義標記圖像

 const marker = new Marker()
 const imageSource = ImageSource.fromFileSync( "~/assets/images/icons/icon.png");
 const icon = new Image();
 icon.imageSource = imageSource;
 marker.icon = icon;

2 b) 使用資源添加自定義標記圖像(例如在 Android 中,這些位於app/App_Resources/Android/src/main/res中的可繪制文件夾中,這里的示例圖像是icon.png

 const marker = new Marker()
 const imageSource = ImageSource.fromResourceSync("icon");
 const icon = new Image();
 icon.imageSource = imageSource;
 marker.icon = icon;

在 Nativescript 版本 7 中,我使用以下代碼使其與本地圖像文件一起使用。

import {ImageSource} from "tns-core-modules/image-source";
import { Image } from "tns-core-modules/ui/image";
import {Folder, path, knownFolders} from "tns-core-modules/file-system"; 

/** Then in the point of adding the marker **/
const folder: Folder = <Folder>knownFolders.currentApp();
const folderPath: string = path.join(folder.path, "images/marker-blue.png");
const imageFromLocalFile: ImageSource = ImageSource.fromFileSync(folderPath);
let image = new Image();
image.imageSource = imageFromLocalFile;
marker.icon = image;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM