经度和纬度获取(Android)+反向地理编码

时间:2015-07-04 03:10:46

标签: android google-maps geolocation

所以我把几个我在这里和那里找到的片段放在一起;然而,为了制作一个更复杂的应用程序,我曾经能够获得经度和纬度数,但是一旦我尝试将这些数字提供给反向GeoCoding机制,我就无法获得Long和单击按钮(执行反向地理编码)后拉取数字和应用程序崩溃

package com.example.com.example;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button myLocation;
    TextView myAddress;
    TextView textLat;
    TextView textLong;

    double latHolder = 0.0;
    double longHolder = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myLocation= (Button) findViewById(R.id.location);
        myAddress = (TextView)findViewById(R.id.address);

        textLat = (TextView)findViewById(R.id.textLat); ///COORDINATION VARIABLES1
        textLong = (TextView)findViewById(R.id.textLong);///2

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //gets it from the Operating system
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); //LOCATION UPDATED LINKED
        if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            textLat.setText("GPS ONLINE (PLEASE WAIT)");
            textLong.setText("GPS ONLINE (PLEASE WAIT)");
        }
        else 
        {
            textLat.setText("GPS OFFLINE");
            textLong.setText("GPS OFFLINE");
        }

        myLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getMyLocationAddress();
            }
        });

    }
    ///////////COORDIATION ACQUIRING DOWN HERE
    private class mylocationlistener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location != null)
            {
                double pLat = location.getLatitude();
                double pLong = location.getLongitude();

                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));

                latHolder = pLat;
                longHolder = pLong;
            }

        }

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }


    }

    public void getMyLocationAddress() {

        Geocoder geocoder= new Geocoder(this, Locale.ENGLISH);

        try {

              //Place your latitude and longitude
              List<Address> addresses = geocoder.getFromLocation(latHolder,longHolder, 1);


              if(addresses != null) {

                  Address fetchedAddress = addresses.get(0);
                  StringBuilder strAddress = new StringBuilder();

                  for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
                        strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
                  }

                  myAddress.setText("I am at: " +strAddress.toString());

              }

              else
                  myAddress.setText("No location found..!");

        } 
        catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 Toast.makeText(getApplicationContext(),"Could not get address..!", Toast.LENGTH_LONG).show();
        }
    }
}  

这是我的Manafist,拥有足够的所有权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

并且最后但不租用我的xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LATITUDE" />

    <Button
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/address"
        android:layout_below="@+id/address"
        android:layout_marginTop="79dp"
        android:text="get_location" />

    <TextView
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_below="@+id/title"
        android:layout_marginTop="135dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_below="@+id/title"
        android:layout_marginTop="44dp"
        android:text="LONGITUD" />

    <TextView
        android:id="@+id/textLong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="26dp" />

    <TextView
        android:id="@+id/textLat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_below="@+id/title" />

</RelativeLayout>

请注意!我添加了两个名为LongHolder和LatHolder的全局变量,将GPS的值直接传递给反向地理编码。我相信他们没有达到目的,但如果你需要自己测试随机选择的坐标,请记得将它们删除。谢谢你

0 个答案:

没有答案