javascript - Google Maps API NativeScript custom marker icon - Stack Overflow

I try to create a marker with a custom icon with NativeScript and the Google maps API package. I added

I try to create a marker with a custom icon with NativeScript and the Google maps API package. I added my icon to the android resources and then built my project a few times.

   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);

Even when I try icon or logo for the ressource it doesn't appear.

I try to create a marker with a custom icon with NativeScript and the Google maps API package. I added my icon to the android resources and then built my project a few times.

   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);

Even when I try icon or logo for the ressource it doesn't appear.

Share Improve this question edited Apr 16, 2018 at 12:26 zdenek 24.6k2 gold badges18 silver badges35 bronze badges asked Nov 26, 2016 at 2:32 FreshchrisFreshchris 1,2514 gold badges19 silver badges35 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Using 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);
    }

Using Vue this worked for me.

Note 1: fromResource and fromFile have been deprecated and new functions are called fromFileSync and fromResourceSync

Note 2: Do not change marker color after setting the icon, this will show you the default icon although you would have set ImageSource for custom icon correctly

1 ) import required NativeScript libraries

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

2 a) Add custom marker image using local path

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

OR

2 b) Add custom marker image using resources (in Android for example these live in drawable folders in app/App_Resources/Android/src/main/res, here the example image is icon.png)

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

In Nativescript version 6 i used the following code to make it work with local image file.

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744394175a4572056.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信