【问题标题】:Google Maps application works on emulator but not real device谷歌地图应用程序可以在模拟器上运行,但不能在真实设备上运行
【发布时间】:2014-08-20 04:20:55
【问题描述】:

我是这里的新用户,我的声誉不足以显示我的应用程序的一些图像以及我的问题,这将使它更加详尽,所以不幸的是,只是在没有任何应用程序图像的情况下提出问题

我正在使用 google nexus 4 ..API 18 (android version 4.3) 虚拟设备,并且一直在开发这个应用程序,它可以在任何具有 api 11 或 11+ 的 android 设备上运行,并且已经在真正的 android 设备上测试了这个应用程序 (三星galaxy S2 lite)但面临一些问题

1) 每当用户在编辑文本框中输入任何位置名称并单击同一活动中的查找按钮时,都会在下面的地图上绘制一个标记.. 它每次在虚拟设备中都可以正常工作,但在 android 上此时,当用户单击查找按钮应用程序时,设备会崩溃。

2) 在这里的第二个活动中,当用户单击它时,我创建了一个按钮我的位置..它将获取设备的 gps 坐标并对其进行反向地理编码,并在其中一个编辑文本中显示位置地址盒子。这在虚拟设备中也可以正常工作。做每件事都需要一点时间,但在真实设备中,当我点击左上角的图标时,它不会显示任何东西,它会显示使用 gps 进行搜索,但不显示任何东西为什么在这里花费太多时间.. 所以这是我在 android 设备上面临的两个问题,但它们在虚拟设备上运行良好..

请帮帮我,我正在处理我的 fyp 并挂在这里

问题1的代码如下

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));


            }           
        }   

}
    }

这是我可以访问该位置的文件

   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

    }



}

【问题讨论】:

  • 尝试为您的问题选择更好的标题和关键字是一种很好的做法。这样一来,您就更有可能吸引对您的问题有所了解的人(在您的情况下是 Google 地图)的注意

标签: android google-maps


【解决方案1】:

在第二个活动中,真实设备需要很长时间才能获取您的位置(2-10 分钟),具体取决于您是在户外还是在家里。 模拟器使用 DDMS 中的模拟器控件获取位置坐标,因此不会花费超过几秒钟的时间。尝试在露天测试您的应用程序。

【讨论】:

  • 但是如果我使用谷歌地图应用程序并在同一位置单击应用程序中的位置图标,它会立即在地图上显示描述您的位置的图标,为什么不同应用程序的行为不同在同一个位置...那问题没有 1 你有什么解决方案吗???
  • 实际上我在我的应用程序中没有使用谷歌地图,我没有使用谷歌地图 API,但是对于第二个问题,您获取位置的方式可能有问题。将您所有的应用程序文件发送到 hadishaheen@hotmail.com 我会尽力找到解决方案!
  • 我还将发布该文件的代码,我正在访问上面的位置观看它
  • 我检查了代码,但我不知道 Google Maps Api 对此感到抱歉:/
  • 没问题的兄弟感谢您的时间和建议
猜你喜欢
  • 2016-02-02
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-12
  • 2016-09-02
相关资源
最近更新 更多