在android中获取双数据类型的lat

时间:2014-11-16 06:40:02

标签: android google-maps

我正在使用谷歌地图。它工作正常,我在标记选项上获得拉特长。但我想在其他活动中传递那个拉特。所以我需要在双数据类型中更改它。因为我必须提供该特定位置的预测信息.Plz帮助我。我的代码在哪里。

  package com.example.gpstracking;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
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.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

@SuppressLint("NewApi")
public class MapActivity1 extends Activity {
    private static final String TAG_LAT = "LAT";
    private static final String TAG_LONG = "LONGITUDE";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_activity1);
        MapFragment fm = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.fragment1);

        // Getting Map for the SupportMapFragment
        final GoogleMap map = fm.getMap();
        // Setting a click event handler for the map
        map.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng latLng) {

                // Creating a marker
                MarkerOptions markerOptions = new MarkerOptions();

                // Setting the position for the marker
                markerOptions.position(latLng);

                // Setting the title for the marker.
                // This will be displayed on taping the marker
                markerOptions.title(latLng.latitude + " : " + latLng.longitude);

                // Clears the previously touched position
                map.clear();

                // Animating to the touched position
                map.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                // Placing a marker on the touched position
                map.addMarker(markerOptions);
                double lat=latLng.latitude;
                double longitude=latLng.longitude;
                String LAT=String.valueOf(lat);
                String LONGITUDE=String.valueOf(longitude);
                Intent in = new Intent(getApplicationContext(),
                        LocationActivity.class);
                // Sending lat/long to next activity
                in.putExtra(TAG_LAT, LAT);
                in.putExtra(TAG_LONG, LONGITUDE);
                startActivityForResult(in, 100);

            }
        });
    }

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

}

2 个答案:

答案 0 :(得分:0)

你有没有尝试过以下的事情?

转换为字符串并传递给另一个activity

String dataToSend=LatLng.toString();

如果您将double传递给另一个activity,请将字符串转换为双倍

double dataToSend=Double.valueof(LatLng.toString());

最后

如果要发送原始数据类型,请使用intents发送数据,否则使用parcelableserializable(对于java对象)传递数据

假设您正在发送原始数据:

Intent int=new Intent(this,newActivity.class)
int.putExtra("key",dataToSend);
StartActivity(int);

然后检索newActivity.class

中的数据

答案 1 :(得分:0)

LocationListener locationListener = new LocationListener(){

                //private String stadion;

                @Override
                public void onLocationChanged(Location location) 
                {
                    float lat = (float)location.getLatitude();
                    float lon = (float)location.getLongitude();

                    map.addMarker(new MarkerOptions()
                    .title(stadion)
                    .position(new LatLng(lat,lon)));
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon), 10)); 

                    for(int i = 0; i < alleStadien.getLength(); i++){
                        Node stadion = alleStadien.item(i);

                        String name = stadion.getAttributes()
                                .getNamedItem(name)
                                .getNodeValue();
                        String verein = stadion.getAttributes()
                                .getNamedItem(verein)
                                .getNodeValue();
                        lat = (float)Float.valueOf(stadion.getAttributes()
                                .getNamedItem(lt)// Fehler
                                .getNodeValue());
                        lon = (float)Float.valueOf(stadion.getAttributes()
                                .getNamedItem(lg)// Fehler
                                .getNodeValue());
                        map.addMarker(new MarkerOptions()
                        .title(name)
                        .snippet(verein)
                        .position(new LatLng(lat,lon)));
                    }
                }

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}

                public void onStatusChanged(String provider, int status,
                        Bundle extras) {}


            };
                geolocation.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 5, locationListener);
            }

            catch(ParserConfigurationException e){}
            catch(SAXException e){}
            catch(IOException e){}

}