谷歌地图应用软件可在仿真器,但不是真正的设备 [英] Google Maps application works on emulator but not real device

查看:199
本文介绍了谷歌地图应用软件可在仿真器,但不是真正的设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这里的新用户,我的名声是不够的,我的问题一起显示我的应用程序的一些图像这将使它更煞费苦心所以很遗憾只是问问题,而不应用程序的任何图像

我使用谷歌Nexus 4 ..API 18(Android版4.3)虚拟设备,并一直在开发这个应用程序可能在任何Android设备上运行的API有11或11+,并有测试真正的Andr​​oid设备上这个应用程序(三星Galaxy S2精简版),但面临着一些问题,这是

1),只要用户在编辑文本框中输入任何地点名称,然后单击在同一活动查找按钮标记绘制如下..它每次都工作正常,在虚拟设备中的地图上,但是在Android设备在这一点时查找按钮的应用日益用户点击获取崩溃。

2)在这里的第二个活动我已经做了我的按钮位置时它曾经用户点击..它会得到设备的GPS坐标和反向地理code,并在一个显示位置ADRESS的编辑文本框。这也。它花一点时间来做好每一件事情,但在实际设备时,我曾经在上顶部的图标点击它不会显示任何东西的虚拟设备工作正常,把它展示了使用搜索GPS,但确实不显示为什么它是花费过多时间在这里任何事情..
所以这些都是我对着Android设备上,但这些虚拟设备上完美工作的两个问题。

plzzz帮助我有我的工作我的五年计划,并得到流连于此

在code为问题1号是在这里如下

 包com.example.citytourguide;
进口java.io.IOException异常;
进口的java.util.List;进口com.google.android.gms.maps.CameraUpdateFactory;
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.google.android.gms.maps.model.LatLng;
进口com.google.android.gms.maps.model.Marker;
进口com.google.android.gms.maps.model.MarkerOptions;
进口android.content.Context;
进口android.content.Intent;
进口android.location.Address;
进口android.location.Geo codeR;
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类导游扩展FragmentActivity {    GoogleMap的GoogleMap的;
    标记标志;
    经纬度的latLng;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_tour_guide);        SupportMapFragment supportMapFragment =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。
               GoogleMap的= supportMapFragment.getMap();          按钮btn_route =(按钮)findViewById(R.id.route);
          btn_route.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                startActivity(新意图(getBaseContext(),ShowPath.class));
            }
        });
     //获取参考btn_find布局activity_main的
        按钮btn_find =(按钮)findViewById(R.id.btn_find);
        btn_find.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                的EditText loc_name =(EditText上)findViewById(R.id.editText1);
                。字符串位置= loc_name.getText()的toString();                如果(location.equals()){
                    Toast.makeText(getBaseContext(),请输入有事找......!,Toast.LENGTH_SHORT).show();                }
                其他{                    ConnectivityManager connMgr =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                    的NetworkInfo NETWORKINFO = connMgr.getActiveNetworkInfo();
                    布尔isWiFi = networkInfo.getType()== ConnectivityManager.TYPE_WIFI;
                    如果(isWiFi ==真)
                    {
                        新的地缘coderTask()执行(位置);
                        }
                    其他{
                        Toast.makeText(getBaseContext(),哎呀错误在网络连接......!,Toast.LENGTH_LONG).show();
                    }                }
            }
        });    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.tour_guide,菜单);
        返回true;
    }    //一个AsyncTask的类访问地址解析网络服务
    保护类地理coderTask延伸的AsyncTask<弦乐,太虚,列表与LT;地址>> {        @覆盖
        保护列表与LT;地址> doInBackground(字符串... LOCATIONNAME){
            //创建地理codeR classtr​​y的一个实例
            地理codeR地理codeR =新的地缘codeR(getBaseContext());
            清单<地址>地址= NULL;            尝试{
                //获取最多3个地址,输入文本相匹配
                地址=地理coder.getFromLocationName(LOCATIONNAME [0],3);
            }赶上(IOException异常五){
                e.printStackTrace();
            }
            返回地址;
            }        @覆盖
        保护无效onPostExecute(列表<地址>地址){            如果(地址== NULL || addresses.size()== 0){
                Toast.makeText(getBaseContext(),无位置找到Toast.LENGTH_SHORT).show();
            }            //清除地图上所有的现有标记
            googleMap.clear();            //添加谷歌地图上标记为每个匹配的地址
            的for(int i = 0; I< addresses.size();我++){                地址地址=(地址)addresses.get(I)                //创建的GeoPoint的一个实例,在谷歌地图中显示
                的latLng =新经纬度(address.getLatitude(),address.getLongitude());                串addressText =的String.format(%S%S,address.getMaxAddressLineIndex()大于0 address.getAddressLine(0):,address.getCountryName());                //显示在地图上标记
                标记标志= googleMap.addMarker(。新的MarkerOptions()位置(的latLng).title伪(addressText));
                mark.showInfoWindow(); //上的标记显示标题                //找到的第一个位置
                如果(我== 0)
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(的latLng));
            }
        }}
    }

这是在那里我有访问该位置的文件

 包com.example.citytourguide;进口java.io.IOException异常;
进口的java.util.List;进口com.google.android.gms.maps.model.LatLng;进口android.location.Address;
进口android.location.Criteria;
进口android.location.Geo codeR;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.provider.Settings;
进口android.app.AlertDialog;
进口android.app.Dialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.support.v4.app.FragmentActivity;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.AutoCompleteTextView;
进口android.widget.Button;
进口android.widget.Toast;
公共类ShowPath扩展FragmentActivity实现OnClickListener,LocationListener的{
    AutoCompleteTextView auto_to,auto_from开启时才有用;
    按钮btn_path,btn_loc;
    双纬度,经度;
     INT I = 1;
     INT A = 1;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_show_path);        //获取参考btn_find布局activity_main的
        auto_from开启时才有用=(AutoCompleteTextView)findViewById(R.id.from);
        auto_to =(AutoCompleteTextView)findViewById(R.id.to);
        auto_from.setText(NULL);
        auto_to.setText(NULL);
        btn_path =(按钮)findViewById(R.id.path);
        btn_loc =(按钮)findViewById(R.id.btn_loc);
        btn_path.setOnClickListener(本);
        btn_loc.setOnClickListener(本);
    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.show_path,菜单);
        菜单项菜单项= menu.add(Menu.NONE,Menu.FIRST,Menu.NONE,保存);
        menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        返回true;
    }
    公共布尔onMenuItemSelected(INT FEATUREID,菜单项项)    {
        Toast.makeText .show()(getBaseContext()+至,Toast.LENGTH_SHORT地图已经从结束的);
        返回super.onMenuItemSelected(FEATUREID,项目);
    }
    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        开关(v.getId()){
        案例R.id.path:
            从= auto_from.getText()字符串的toString();
            。字符串= auto_to.getText()的toString();            如果(to.equals()及与放大器; from.equals()){
                Toast.makeText(getBaseContext(),请输入正确启动和目标......,Toast.LENGTH_SHORT).show();            }
            其他{
                ConnectivityManager connMgr =(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
                的NetworkInfo NETWORKINFO = connMgr.getActiveNetworkInfo();
                布尔isWiFi = networkInfo.getType()== ConnectivityManager.TYPE_WIFI;                如果(isWiFi ==真)
                {
                    在意向=新意图(ShowPath.this,ShowMap.class);                    in.putExtra(开始,来自);
                    in.putExtra(目标,到);
                    startActivity(在);                }
                其他{
                    Toast.makeText(getBaseContext(),哎呀错误在网络连接......!,Toast.LENGTH_LONG).show();
                }            }
            打破;
        案例R.id.btn_loc:
            //从系统服务LOCATION_SERVICE获取的LocationManager对象
            的LocationManager的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);            //创建一个标准的对象来检索提供商
            标准标准=新标准();            //获得最佳供应商的名称
            字符串提供商= locationManager.getBestProvider(标准,真正的);
            地点= NULL;
            如果(provider.contains(GPS))
            {
                //获取当前位置的GPS
                如果(auto_from.getText()的toString()的比赛(。)及和放大器;(auto_to.getText()的toString()的比赛())){
                    I = 0;
                }
                否则,如果(auto_to.getText()。的toString()。匹配()){
                    一个= 0;
                }                位置= locationManager.getLastKnownLocation(供应商);
                如果(位置!= NULL)
                    onLocationChanged(位置);            }
            其他
            {
                的ShowDialog(0);            }
            locationManager.requestLocationUpdates(供应商,1000,0,这一点);
            //locationManager.requestLocationUpdates(provider,20000,0,这一点);
            打破;
        默认:
            打破;        } // switch语句结束
    } //在点击FUNC结束    @覆盖
    保护对话框onCreateDialog(INT ID){        最终字符串消息=启用GPS或其他任何位置
                +服务来查找当前位置,点击OK去
                +定位服务设置,让你这样做。
        返回新AlertDialog.Builder(本).setMessage(消息)
                .setTitle(警告)
                .setPositiveButton(OK,新DialogInterface.OnClickListener(){                    公共无效的onClick(DialogInterface对话,诠释whichButton)
                    {
                        意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(意向);
                    }
                })
                .setNegativeButton(取消,新DialogInterface.OnClickListener(){                    公共无效的onClick(DialogInterface对话,诠释whichButton)
                    {
                        Toast.makeText(getBaseContext(),
                                全球定位系统服务仍然禁用,Toast.LENGTH_SHORT).show();
                    }
                })。创建();    }    @覆盖
    公共无效onLocationChanged(地点){
        // TODO自动生成方法存根
        纬度= location.getLatitude();
        LNG = location.getLongitude();
        点的LatLng =新经纬度(纬度,经度);
        //转换用户位置,以解决并指定为自动完成文本框
        新ReverseGeocodingTask(getBaseContext())执行(点);
    }    私有类ReverseGeocodingTask扩展的AsyncTask<经纬度,太虚,字符串> {
        上下文mContext;        公共ReverseGeocodingTask(上下文的背景下){
            超();
            mContext =背景;
        }        //使用反向地理编码查找地址
        @覆盖
        保护字符串doInBackground(经纬度... PARAMS){
            地理codeR地理codeR =新的地缘codeR(mContext);
            双纬度=参数[0] .latitude;
            双经度=参数[0] .longitude;
            清单<地址>地址= NULL;
            串addressText =;            尝试{
                地址=地理coder.getFromLocation(纬度,经度,1);
            }赶上(IOException异常五){
                e.printStackTrace();
            }            如果(地址=空&放大器;!&放大器; addresses.size()大于0){
                地址地址= addresses.get(0);                addressText =的String.format(%S%S%S,
                        address.getMaxAddressLineIndex()> 0? address.getAddressLine(0):,
                                address.getLocality(),
                                address.getCountryName());
            }
            返回addressText;
        }        @覆盖
        保护无效onPostExecute(字符串addressText){
            如果(我== 0){
                auto_from.setText(addressText);
            我++;
            }
            否则如果(一个== 0){
                Log.d(阿西,+α);
                auto_to.setText(addressText);
            一个++;
            }
        } //执行后完成
    } //类反向地理编码完成    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        // TODO自动生成方法存根    }
    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        // TODO自动生成方法存根    }
    @覆盖
    公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
        // TODO自动生成方法存根    }}


解决方案

在第二个活动真正的设备需要太多的时间来取,如果你是在露天或在家里根据您的位置(2-10分钟)。
该emulater使用需要在DDMS的Emulater控制,位置坐标,它不会采取比seconds.Try更露天测试乌尔应用程序。

I am a new user here and my reputation is not enough to display some images of my app along with my question which would make it more elaborative so unfortunately just asking the question without any images of app

i am using google nexus 4 ..API 18 (android version 4.3) virtual device and had been developing this app which could run on any android device having api 11 or 11+ and have test this app on the real android device (Samsung galaxy S2 lite) but facing some problems which are

1) whenever user will enter any location name in the edit text box and click on the find button in the same activity a marker is drawn on the map below.. it works fine in the virtual device every time but on the android device at this point when ever user clicks on find button application get crash.

2) in the the second activity here i have made a button my location when ever user clicks on it ..it will get the gps coordinates of the device and reverse geocode it and display the location adress in one of the edit text boxes .this also works fine in virtual device .it take a moment to do every thing but in real device it does not display any thing when ever i click on the icon at the top left it shows search using gps but does not display any thing why it is taking too much time here .. so these are the two problems which i am facing on the android device but these are working perfectly on the virtual device..

plzzz help me there i am working on my fyp and get hang here

the code for the problem no 1 is here as follow

package com.example.citytourguide;
import java.io.IOException;
import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TourGuide extends FragmentActivity {

    GoogleMap googleMap;
    Marker mark;
    LatLng latLng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tour_guide);

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

          Button btn_route=(Button) findViewById(R.id.route);
          btn_route.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getBaseContext(),ShowPath.class));
            }
        });


     // Getting reference to btn_find of the layout activity_main
        Button btn_find = (Button) findViewById(R.id.btn_find);
        btn_find.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                EditText loc_name = (EditText) findViewById(R.id.editText1);
                String location = loc_name.getText().toString();

                if(location.equals("")){
                    Toast.makeText(getBaseContext(), "Please! Enter Something to Find...", Toast.LENGTH_SHORT).show();

                }
                else{

                    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                    boolean isWiFi = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
                    if (isWiFi==true)
                    {  
                        new GeocoderTask().execute(location);
                        }
                    else {
                        Toast.makeText(getBaseContext(), "Ooops! Error In Network Connection...", Toast.LENGTH_LONG).show();
                    }

                }
            }
        });

    }


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

    // An AsyncTask class for accessing the GeoCoding Web Service
    protected class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

        @Override
        protected List<Address> doInBackground(String... locationName) {
            // Creating an instance of Geocoder classtry
            Geocoder geocoder = new Geocoder(getBaseContext());
            List<Address> addresses = null;

            try {
                // Getting a maximum of 3 Address that matches the input text
                addresses = geocoder.getFromLocationName(locationName[0], 3);
            } catch (IOException e) {
                e.printStackTrace();
            }           
            return addresses; 
            }

        @Override
        protected void onPostExecute(List<Address> addresses) {         

            if(addresses==null || addresses.size()==0){
                Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
            }

            // Clears all the existing markers on the map
            googleMap.clear();

            // Adding Markers on Google Map for each matching address
            for(int i=0;i<addresses.size();i++){                

                Address address = (Address) addresses.get(i);

                // Creating an instance of GeoPoint, to display in Google Map
                latLng = new LatLng(address.getLatitude(), address.getLongitude());

                String addressText = String.format("%s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",address.getCountryName());

                // Showing the marker on the Map
                Marker mark =googleMap.addMarker(new MarkerOptions().position(latLng).title(addressText));
                mark.showInfoWindow(); // displaying title on the marker          



                // Locate the first location
                if(i==0)
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));


            }           
        }   

}
    }

this is the file where i have access the location

   package com.example.citytourguide;

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

import com.google.android.gms.maps.model.LatLng;

import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;


public class ShowPath extends FragmentActivity implements OnClickListener,LocationListener {


    AutoCompleteTextView auto_to, auto_from;
    Button btn_path, btn_loc;
    double lat,lng;
     int i = 1 ;
     int a = 1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_path);





        // Getting reference to btn_find of the layout activity_main
        auto_from = (AutoCompleteTextView) findViewById(R.id.from);         
        auto_to = (AutoCompleteTextView) findViewById(R.id.to);
        auto_from.setText(null);
        auto_to.setText(null);
        btn_path=(Button) findViewById(R.id.path);
        btn_loc=(Button) findViewById(R.id.btn_loc);    
        btn_path.setOnClickListener(this);
        btn_loc.setOnClickListener(this);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show_path, menu);
        MenuItem menuitem=menu.add(Menu.NONE,Menu.FIRST,Menu.NONE,"Save");
        menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }
    public boolean onMenuItemSelected(int featureId,MenuItem item)

    {
        Toast.makeText(getBaseContext(),"Map has been concluded from "  + " to " , Toast.LENGTH_SHORT).show();
        return super.onMenuItemSelected(featureId, item);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.path:
            String from=auto_from.getText().toString();
            String to = auto_to.getText().toString();

            if(to.equals("")&& from.equals("")){
                Toast.makeText(getBaseContext(), "Please! Enter Start and Destination properly...", Toast.LENGTH_SHORT).show();

            }
            else{
                ConnectivityManager connMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                boolean isWiFi = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;

                if (isWiFi==true)
                { 
                    Intent in = new Intent(ShowPath.this,ShowMap.class);

                    in.putExtra("Start", from);
                    in.putExtra("Dest", to);
                    startActivity(in);

                }
                else {
                    Toast.makeText(getBaseContext(), "Ooops! Error In Network Connection...", Toast.LENGTH_LONG).show();
                }

            }
            break;
        case R.id.btn_loc:
            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
            Location location = null ;


            if(provider.contains("gps"))
            {
                // Getting Current Location From GPS
                if(auto_from.getText().toString().matches("") && (auto_to.getText().toString().matches(""))){
                    i=0 ;
                }
                else if (auto_to.getText().toString().matches("")) {
                    a=0;
                }

                location = locationManager.getLastKnownLocation(provider);
                if(location!=null)
                    onLocationChanged(location);

            }
            else
            {
                showDialog(0);

            }
            locationManager.requestLocationUpdates(provider, 1000, 0, this);    
            //locationManager.requestLocationUpdates(provider, 20000, 0, this);
            break;
        default:
            break;

        }   //switch statement ends


    } // on click func ends

    @Override
    protected Dialog onCreateDialog(int id){

        final String message = "Enable either GPS or any other location"
                + " service to find current location.  Click OK to go to"
                + " location services settings to let you do so.";
        return new AlertDialog.Builder(this).setMessage(message)
                .setTitle("Warning")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,int whichButton)
                    {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,int whichButton)
                    {
                        Toast.makeText(getBaseContext(),
                                "Gps services still disabled", Toast.LENGTH_SHORT).show();
                    }
                }).create();

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        lat = location.getLatitude();
        lng = location.getLongitude();
        LatLng point = new LatLng(lat,lng);
        // converting user location to address and assign it to autocomplete text box
        new ReverseGeocodingTask(getBaseContext()).execute(point);
    }

    private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
        Context mContext;

        public ReverseGeocodingTask(Context context){
            super();
            mContext = context;
        }

        // Finding address using reverse geocoding
        @Override
        protected String doInBackground(LatLng... params) {
            Geocoder geocoder = new Geocoder(mContext);
            double latitude = params[0].latitude;
            double longitude = params[0].longitude;
            List<Address> addresses = null;
            String addressText="";

            try {
                addresses = geocoder.getFromLocation(latitude, longitude,1);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(addresses != null && addresses.size() > 0 ){
                Address address = addresses.get(0);

                addressText = String.format("%s, %s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                                address.getLocality(),
                                address.getCountryName());
            }
            return addressText;
        }

        @Override
        protected void onPostExecute(String addressText) {
            if(i==0){
                auto_from.setText(addressText);
            i++ ;
            }
            else if(a==0){
                Log.d("asim","" + a);
                auto_to.setText(addressText);
            a++;
            }


        }// post execute finish
    }// class reverse geocoding finish



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

    }


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

    }


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

    }



}

解决方案

In the second activity the real device needs much time to fetch your location (2-10 mins) depending if you are in open air or in the house. The emulater takes the location coordinates using the Emulater control in the DDMS so, it won't take more than seconds.Try testing ur app in open air.

这篇关于谷歌地图应用软件可在仿真器,但不是真正的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆