android - 在 Google map v2 Android 上添加标记

标签 android google-maps marker

我按照中的教程学习了

https://developers.google.com/maps/documentation/android/start

我设法将 map 加载到我的应用程序中。现在我想使用纬度和经度将一些标记添加到我的应用程序中。我该怎么做?我尝试了一些但没有用。我试过的代码如下。

我本来打算这样做的。但是我得到了一个空点异常。这是什么原因?

GoogleMap googleMap = null;

           MapFragment fm = (MapFragment) (activity.getFragmentManager())
                    .findFragmentById(R.id.map);

           googleMap = fm.getMap();

            double latitude = 7.421226;
            double longitude =80.401264 ;

            // create marker
            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");

            // adding marker
            googleMap.addMarker(marker); 

我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/filmhall"
    android:background="@color/grey" >



<fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/> 

</LinearLayout>

最佳答案

使用这个方法看看我们在谷歌地图上添加标记

mMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore"));

举个例子

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class BasicMapDemoActivity extends FragmentActivity
{
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basic_demo);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() 
    {
        if (mMap == null) 
        {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            if (mMap != null) 
            {
                setUpMap();
            }
        }
    }

    private void setUpMap() 
    {
        mMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore"));
  // here is marker Adding code
    }
}

Example on Google Map

关于android - 在 Google map v2 Android 上添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27719572/

相关文章:

android - 将 Kotlin @Parcelize 与多态一起使用

android - 将数据库中的特定列检索到 Spinner

javascript - 从 mysql 检索纬度和经度,将其传递给谷歌地图 api

iphone - 如何使用 GeoLocation 在 map 上定位标记?

android - CSS3 背景位置动画在移动设备上滞后

android - 针对 android 的 libusb 进行编译和链接

google-maps - 如何从给定经纬度矩形的谷歌地图中提取卫星图像?

ios - 我的位置按钮未出现在 View 中(Google map SDK、ios)

javascript - 如何在传单中的标记下方添加文本?

AndroidPlot 隐形标记