如何相对于用户当前位置放置标记?

时间:2014-12-12 22:59:59

标签: android maps

您好我是Android新手,我正在尝试编写一个应用程序,该应用程序显示相对于平板电脑当前位置的地图和标记(代表人物),例如视频游戏中的HUD。一个人的位置以xy坐标给出,所以如果一个人在前面4英尺,在左边两英尺,他们的坐标将是(-2,4)。

现在我正试图在当前位置的顶部放置一个标记,但我遇到了一些麻烦。我有地图工作,我已经为当前位置添加了自定义标记和蓝点但我不能让标记跟随蓝点

  private void setUpMap() {
    View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custommarker, null);
    TextView numTxt = (TextView) marker.findViewById(R.id.num_txt);
    numTxt.setText("Dave");
    mMap.setMyLocationEnabled(true);
    double dLatitude =mCurrentLocation.getLatitude();
    double dLongitude = mCurrentLocation.getLongitude();
    markerLatLng = new LatLng(dLatitude, dLongitude);

    customMarker = mMap.addMarker(new MarkerOptions()
            .position(markerLatLng)
            .title("Title")
            .snippet("Description")
            .icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(this, marker))));

我错过了什么或做错了什么?

这里是logcat

                java.lang.NullPointerException
        at com.google.android.gms.maps.model.LatLngBounds$Builder.include(Unknown Source)
        at com.google.android.gms.location.sample.locationupdates.MainActivity$1.onGlobalLayout(MainActivity.java:398)
        at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

标记不是相对的,它们是固定的。如果您希望跟踪用户位置,则必须在用户位置更改时更新它。

相关问题