【发布时间】:2014-01-20 15:32:50
【问题描述】:
我尝试在 google maps api 与 google places api 组合中搜索我的位置,我只看到地点制造商,但我不打印我的地理位置。
(对不起,我的英语……我希望能理解我……)
我的代码在一个片段中:
public class MapFragmentActivity extends Fragment implements LocationListener {
private static View gv;
GoogleMap googleMap;
List<Place> findPlaces;
GPSLocation gps;
Handler mHandler = new Handler();
private ActionBar actionBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (gv != null) {
ViewGroup parent = (ViewGroup) gv.getParent();
if (parent != null){
parent.removeView(gv);
}
}
try {
gv = inflater.inflate(R.layout.principalayoutmap,container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
gv.setBackgroundResource(android.R.color.black);
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
gps = new GPSLocation(gv.getContext());
if(gps.canGetLocation()){
new GetPlaces().execute();
googleMap.setMyLocationEnabled(true);
}
else{
gps.showSettingsAlert();
}
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()),100);
googleMap.moveCamera(update);
return gv;
}
public void onDestroyView() {
super.onDestroyView();
//Log.d(TAG, "onDestroyView");
googleMap.clear();
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
class GetPlaces extends AsyncTask<Void, Void, Void>{
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//this.listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, placeName));
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
mHandler.post(new Runnable() {
@Override
public void run() {
findNearLocation();
}
});
return null;
}
}
public void findNearLocation() {
//googleMap.setMyLocationEnabled(true);
PlacesService service = new PlacesService("****xvRF1RH4tMq1************");
double lat = gps.getLatitude();
double lon = gps.getLongitude();
findPlaces = service.findPlaces(lat,lon,"food");
for(int i=0;i<findPlaces.size();i++){
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(findPlaces.get(i).getLatitude(), findPlaces.get(i).getLongitude()))
.title(findPlaces.get(i).getName())
.snippet(findPlaces.get(i).getVicinity())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
}
@Override
public void onLocationChanged(Location location) {
}
@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
}
}
GPSLocation.class 是一个用于搜索我的位置并帮助打印附近地点的类
Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zape.zapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- Google Maps related permissions -->
<permission
android:name="com.ecs.google.maps.v2.actionbarsherlock.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
<!-- Network connectivity permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Access Google based webservices -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.loggin.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>
<activity android:name="com.loggin.Registrar"></activity>
<activity android:name="com.principal.PrincipalMainMap"></activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AI***GwDUTs6hb6VE8eoG****"/>
<activity android:name="com.principal.MainFragment"></activity>
</application>
</manifest>
我的 principalMapLayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
</LinearLayout>
出于安全考虑,我删除了我的 API KEYS,这不是问题。
最后,我在我的设备(NEXUS 4)中展示了这个:
可以看到只打印地点标记,但我不打印我的地理位置
【问题讨论】:
-
gps.canGetLocation() 是真的吗?
-
是的,这个类用于地点标记..如果 gps.canGetLocation() 为假,则不打印地点标记..
-
Google 的地图应用会显示您的位置?可能是设备的问题...
-
是可能的......在我出来之前很好,但是当我试图删除我的位置并开始离开我的 GPS Intent 时,激活后不会让我返回应用程序,但 inciarla 再次有让我离开了我的位置,但如果地方标记(我用谷歌翻译写,对不起)
-
你知道删除所有应用程序的任何选项,以避免前者的任何残留物,例如在缓存中??
标签: java android location google-maps-android-api-2 google-places-api