把标记放在纬度和经度上

时间:2014-04-02 13:43:41

标签: android google-maps marker

这是我的代码:

按钮监听器,其中我获取经度和纬度并传递给其他意图:

    find.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent route = new Intent("com.project.simplify.MyLocationDemoActivity");

                    myAddress = "Vilnius, Kalvariju gatve 17";
                    System.out.println(myAddress);


                    try {
                        coder = new Geocoder(getApplicationContext(), Locale.getDefault());
                        address = coder.getFromLocationName(myAddress, 1);
                        //coder.getFromLocationName(myAddress,5);
                        if (address == null) {

                        }
                        Address location = address.get(0);
                       System.out.println( "lat : " + location.getLatitude());

                        System.out.println( "long : " + location.getLongitude());


                        float lat = (float) location.getLatitude();
                        float lng = (float) location.getLongitude();
                        route.putExtra("lat", lat);
                        route.putExtra("lng", lng);


                       startActivity(route);
                    }catch(Exception x){
                        System.out.println("nepavyko gauti koordinaciu + RouteActivity");
                    }
                }
            });

以及我想放置标记的其他活动

public class MyLocationDemoActivity extends FragmentActivity implements
        ConnectionCallbacks, OnConnectionFailedListener, LocationListener,
        OnMyLocationButtonClickListener {

    private GoogleMap mMap;
    GeoPoint gp;
    private LocationClient mLocationClient;
    private TextView mMessageView;

    // These settings are the same as the settings for the map. They will in
    // fact give you updates
    // at the maximal rates currently possible.
    private static final LocationRequest REQUEST = LocationRequest.create()
            .setInterval(5000) // 5 seconds
            .setFastestInterval(16) // 16ms = 60fps
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        float lng = (Float) intent.getSerializableExtra("lng");
        float lat = (Float) intent.getSerializableExtra("lat");
        setContentView(R.layout.my_location_demo);
        mMessageView = (TextView) findViewById(R.id.message_text);


        mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)));

    }

我正确传递了浮点数,但无法找到添加标记的方法 可能是什么问题?

更新仍然是同样的问题

04-02 16:51:55.294: E/AndroidRuntime(11252): FATAL EXCEPTION: main
04-02 16:51:55.294: E/AndroidRuntime(11252): Process: com.project.simplify, PID: 11252
04-02 16:51:55.294: E/AndroidRuntime(11252): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.simplify/com.project.simplify.MyLocationDemoActivity}: java.lang.NullPointerException
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.os.Looper.loop(Looper.java:136)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at java.lang.reflect.Method.invoke(Method.java:515)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at dalvik.system.NativeStart.main(Native Method)
04-02 16:51:55.294: E/AndroidRuntime(11252): Caused by: java.lang.NullPointerException
04-02 16:51:55.294: E/AndroidRuntime(11252):    at com.project.simplify.MyLocationDemoActivity.onCreate(MyLocationDemoActivity.java:57)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.Activity.performCreate(Activity.java:5231)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-02 16:51:55.294: E/AndroidRuntime(11252):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-02 16:51:55.294: E/AndroidRuntime(11252):    ... 11 more
04-02 16:51:55.514: D/dalvikvm(11252): GC_FOR_ALLOC freed 308K, 2% free 20060K/20400K, paused 13ms, total 13ms
04-02 16:51:55.674: W/ActivityThread(11252): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());

2 个答案:

答案 0 :(得分:1)

您的案例中的

mMap对象未初始化!你NullPointerException的理由是什么。

onCreate方法中:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    float lng = (Float) intent.getSerializableExtra("lng");
    float lat = (Float) intent.getSerializableExtra("lat");
    setContentView(R.layout.my_location_demo);
    mMessageView = (TextView) findViewById(R.id.message_text);
    mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)));
}

您已设置内容视图,并尝试立即向地图添加标记,而不首先获取该实例。

<强>更新

你需要做这样的事情:

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

之前可以实际添加标记。 我假设您使用SupportMapFragment对象将地图添加到布局中。

答案 1 :(得分:0)

最好在意图

中发送Lat和Lng而不是标记

//在意图中发送LatLng

intent in=new Intent(source.this,destination.class);
Bundle bundle = new Bundle();
bundle.putParcelable("Location", new LatLng(lat,lng));
in.putExtras(in);
startActivity(in);

//在其他活动中接收LatLng

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
LatLng location = bundle.getParcelable("Location");