在新活动中使用从数组到LatLng的字符串

时间:2016-06-27 07:52:52

标签: android arrays google-maps listview latitude-longitude

我无法解决LatLng的问题,我将其用于将地图放置在地图上。我使用字符串数组中的不同数据(如姓名,电话号码等)。我还把那个地方的纬度和经度放在那里。因此,当用户点击listview上的位置时,他会获得有关不同位置的信息。除了在地图上播种位置外,一切正常。 坐标来自字符串中的String-array:

  <string-array name="latitude">
    <item>XX.XXXXXXX</item>
    <item>XX.XXXXXXX</item>
    <item>XX.XXXXXXX</item>
  </string-array>

  <string-array name="longitude">
    <item>XX.XXXXXXX</item>
    <item>XX.XXXXXXX</item>
    <item>XX.XXXXXXX</item>
   </string-array>

我传递的数据和所有超级工作

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String[] title = getResources().getStringArray(R.array.my_string_list);
        final String name = title[position];   
        String[] address = getResources().getStringArray(R.array.address);
            final String addressplace = adres[position];   
         String[] phone = getResources().getStringArray(R.array.phone);
            final String phonenumber = telefon[position];
         String[] lat = getResources().getStringArray(R.array.latitude);
            final String infolatitude = lat[position];
         String[] longi = getResources().getStringArray(R.array.longitude);
                final String infolongitude = longi[position];

        Intent intent = new Intent(getApplicationContext(), Contact.class);

        intent.putExtra("address", addressplace);
        intent.putExtra("telefon", phonenumber);
        intent.putExtra("lat", infolatitude);
        intent.putExtra("longi", infolongitude);
        startActivity(intent);

在下一个活动中,我尝试在地图中使用Lat + Lng来显示引脚

公共类联系人扩展活动{

private double latitude;
private double longitude;
private final LatLng LOCATION =new LatLng (latitude,longitude);
private GoogleMap map;

String title;  
String address; 
String phone;
String lat; 
String longi; 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact);

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    map.addMarker(new MarkerOptions().position(LOCATION));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(LOCATION, 15));

    TextView tvTitle = (TextView)findViewById(R.id.title_text); 
    TextView tvAddress = (TextView)findViewById(R.id.textView1_address);
    Button btn = (Button)findViewById(R.id.textView2_tel);


    Bundle extras = getIntent().getExtras(); 

    if (extras != null) {

        tvTitle.setText(extras.getString("title")); 
        tvAdres.setText(extras.getString("address"));
        btn.setText(extras.getString("phone"));
        latitude = Double.parseDouble(getIntent().getStringExtra("lat"));
        longitude = Double.parseDouble(getIntent().getStringExtra("longi"));
        }}

地图没有显示正确的位置,但在非洲附近开了一个地方。 我非常感谢您协助制作此代码 - 显示位置 - 工作。

1 个答案:

答案 0 :(得分:2)

The problem is that you have intialized the LOCATION variable with the default value of latitude and longitude i.e perhaps (0.0,0.0) default value of double variable.

Do the following changes to get the updated values from intent and then show it on the map   
 if (extras != null) {

            tvTitle.setText(extras.getString("title")); 
            tvAdres.setText(extras.getString("address"));
            btn.setText(extras.getString("phone"));
            latitude = Double.parseDouble(getIntent().getStringExtra("lat"));
            longitude = Double.parseDouble(getIntent().getStringExtra("longi"));
            }}
    private final LatLng LOCATION =new LatLng (latitude,longitude);
    map.addMarker(new MarkerOptions().position(LOCATION));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(LOCATION, 15));