我想在地图上添加标记并缩放到该位置

时间:2013-10-07 13:02:27

标签: android google-maps google-maps-markers google-maps-api-2

我正在开发应用程序,在其中我可以看到地图并在其上放置一些标记并缩放到该位置。

我成功显示了我的地图视图。但是在添加了标记和缩放代码后,它强行关闭...请指导我,我已经给出了下面的代码以及我的日志猫贴。

package com.appeal.my_map;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.CameraPosition;
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;
import android.view.Menu;



public class MainActivity extends FragmentActivity {
private GoogleMap googlemap;

double latitude = 17.385044;
double longitude = 78.486671;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    marker();

    CameraPosition cameraPosition = new CameraPosition.Builder().target(
            new LatLng(17.385044, 78.486671)).zoom(12).build();

googlemap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}




private void marker() {
    // TODO Auto-generated method stub
    MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
    googlemap.addMarker(marker);
    googlemap.setMyLocationEnabled(true);
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

我的xml文件

<?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" >

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

</LinearLayout>

Log Cat

7561    com.appeal.my_map   AndroidRuntime  FATAL EXCEPTION: main
7561    com.appeal.my_map   AndroidRuntime  java.lang.RuntimeException: Unable     to start activity ComponentInfo{com.appeal.my_map/com.appeal.my_map.MainActivity}:     java.lang.NullPointerException
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.access$600(ActivityThread.java:162)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
7561    com.appeal.my_map   AndroidRuntime  at     android.os.Handler.dispatchMessage(Handler.java:107)
7561    com.appeal.my_map   AndroidRuntime  at     android.os.Looper.loop(Looper.java:194)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.main(ActivityThread.java:5371)
7561    com.appeal.my_map   AndroidRuntime  at     java.lang.reflect.Method.invokeNative(Native Method)
7561    com.appeal.my_map   AndroidRuntime  at     java.lang.reflect.Method.invoke(Method.java:525)
7561    com.appeal.my_map   AndroidRuntime  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
7561    com.appeal.my_map   AndroidRuntime  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
7561    com.appeal.my_map   AndroidRuntime  at     dalvik.system.NativeStart.main(Native Method)
7561    com.appeal.my_map   AndroidRuntime  Caused by:     java.lang.NullPointerException
7561    com.appeal.my_map   AndroidRuntime  at     com.appeal.my_map.MainActivity.marker(MainActivity.java:41)
7561    com.appeal.my_map   AndroidRuntime  at     com.appeal.my_map.MainActivity.onCreate(MainActivity.java:25)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.Activity.performCreate(Activity.java:5122)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
7561    com.appeal.my_map   AndroidRuntime  ... 11 more

请纠正我哪里出错了... 谢谢......!

2 个答案:

答案 0 :(得分:1)

您尚未初始化googlemap。您需要初始化相同的

 SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
 googlemap = fm.getMap(); 

答案 1 :(得分:0)

GoogleMap是空的?尝试先初始化GoogleMap,然后添加标记。否则,您将获得NullPointer异常。

googleMap = (GoogleMap)...findViewById(R.id.....);