【问题标题】:NFC read tag to launch Google Maps with coordinatesNFC 读取标签以启动带有坐标的谷歌地图
【发布时间】:2015-04-24 15:15:37
【问题描述】:

我正在尝试使用以下方式编写带有坐标(纬度和经度)的 NFC 标签:

这是在onCreate()里面:

btnWriteMap.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        String latitude = lat.getText().toString();
        String longitude = lon.getText().toString();            
        urlAddress = "geo:"+latitude+","+longitude;
        TextView messageText = (TextView)findViewById(R.id.txtMessage);
        messageText.setText("Touch NFC Tag to share GEO location\n"+
            "Latitude: "+latitude+"\nLongitude: "+longitude);                   
    }
});

mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

mFilters = new IntentFilter[] {
    ndef,
};

mTechLists = new String[][] { new String[] { Ndef.class.getName() },
    new String[] { NdefFormatable.class.getName() }};

onNewIntent() 方法:

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
    String externalType = "nfclab.com:geoService";
    NdefRecord extRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, externalType.getBytes(), new byte[0], urlAddress.getBytes());
    NdefMessage newMessage = new NdefMessage(new NdefRecord[] { extRecord});
    writeNdefMessageToTag(newMessage, tag);    
}

代码是书中的示例。我已经测试过,标签确实是用 geo:lat,lon 写的,当然还有我在 EditTexts 中的坐标。

当我阅读标签时出现问题。它只会显示(在默认的 Android 标记应用程序上)以下消息:

vnd.android.nfc//ext/nfclab.com:geoService

我想要的是让标签应用程序识别这些是谷歌地图坐标并使用坐标启动地图。 externalType String 应该包含什么? 我是否需要在清单中使用 Intent 过滤器?

【问题讨论】:

    标签: android google-maps geolocation nfc ndef


    【解决方案1】:

    标签应用程序显示无法识别的 NFC 论坛外部类型 nfclab.com:geoService(请注意,外部类型名称只能使用小写字母,请参阅 my answer here),因为您将该记录类型存储在标签上。该类型是由 nfclab.com 创建的自定义类型,绝不是标准化的。因此,标签应用程序不知道它应该如何处理该记录。

    在 NFC 标签上存储地理坐标的标准方法是 geo: URI 方案。因此,您通常会创建一个包含您的地理位置的 URI 记录:URI:

    String geoUri = "geo:" + latitude + "," + longitude;
    NdefRecord geoUriRecord = NdefRecord.createUri(geoUri);
    

    标签应用程序将处理这种类型的 URI,并允许您在任何注册了 geo: 方案的应用程序(例如 Google 地图)中打开 geo: URI。

    【讨论】:

    • 谢谢,现在可以了。你知道我能做什么吗,在我阅读标签后,它不仅将我带到该位置,而且还在该位置放置一个标记(例如在谷歌地图中)?因为否则很难知道写在标签上的精确位置。稍后编辑:我想这也可以通过另一个活动来完成,它读取标签并启动谷歌地图,放置在一个片段中,同时放置一个带有坐标的标记。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多