在Google Maps v2 Android上添加标记 [英] Adding Markers on Google maps v2 Android

查看:112
本文介绍了在Google Maps v2 Android上添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了

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

并且我设法将地图加载到我的应用程序中.现在我想使用纬度和经度在我的应用程序中添加一些标记.我该怎么做?我尝试了一些东西,但是没有用.我尝试过的代码如下.

and i managed to load a map into my app.Now i want to add some markers into my app using latitudes and longitudes.How do i do it? I tried something but did not work. The code i tried is below.

这就是我要怎么做.但是我得到了一个空值异常.这是什么原因?

This how i was going to do it.But i get a null point exception.What is the reason for that?

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>

推荐答案

使用此方法,我们正在Google地图上添加标记

have a look using this methed we are adding marker on google map

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
    }
}

Google地图示例

这篇关于在Google Maps v2 Android上添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆