初始化谷歌地图后重绘片段视图 - 机器人 [英] Redraw Fragment in View after initialising Google Map - Android
问题描述
我想对我的资料查看半个屏幕上显示一个谷歌地图。在我的的onCreate
方法,我创建了一个新实例的的AsyncTask
通过JSON检索经度和纬度,从谷歌服务器。接收结果后,我呼吁:
GoogMapHandler GMH =新GoogMapHandler(fragmentManager,的applicationContext);
gmh.initilizeMap(坐标,用户名,AddressInfo的);
下面是我的 GoogMapHandler
类:
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentTransaction;
进口android.content.Context;
进口android.widget.Toast;
进口android.support.v4.app.FragmentManager;
进口android.util.Log;
进口com.google.android.gms.internal.fm;
进口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;
公共类GoogMapHandler {
// 谷歌地图
私人GoogleMap的GoogleMap的;
私人android.support.v4.app.FragmentManager fragmentManager;
民营背景的applicationContext;
公共GoogMapHandler(FragmentManager fmanager,上下文的applicationContext){
this.fragmentManager = fmanager;
this.applicationContext =的applicationContext;
}
公共无效initilizeMap(经纬度坐标,字符串username,字符串addrinfo中){
如果(GoogleMap的== NULL){
片段片段= this.fragmentManager.findFragmentById(R.id.map);
FragmentTransaction fragTransaction = this.fragmentManager
.beginTransaction();
fragTransaction.detach(片段);
SupportMapFragment supportmapfragment =(SupportMapFragment)片段;
GoogleMap的= supportmapfragment.getMap();
如果(GoogleMap的== NULL){
Toast.makeText(ApplicationContext一
对不起!无法创建映射。Toast.LENGTH_SHORT)
。显示();
} 其他 {
标记LOC = googleMap.addMarker(新MarkerOptions()
.position(坐标).title伪(用户:+用户名)
是.snippet(addrinfo中));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(坐标,
15));
fragTransaction.attach(片段);
fragTransaction.commit();
}
}
}
}
现在的问题是,我不能看地图,虽然我没有得到任何错误。当查看已经创建,我试图以重绘的片段显示在地图上,但它不工作。
我怎样才能做到这一点?
任何帮助将是很大的AP preciated。
编辑:
下面是我的 ProfileActivity
。正如所建议的,我用 onResume
创建的AsyncTask
。
公共类ProfileActivity扩展BaseActivity {
私人字符串的用户名,地址,邮寄code,国家;
@覆盖
保护无效onResume(){
Log.i(URL,
http://maps.googleapis.com/maps/api/geo$c$c/json?address=
+ address.replaceAll(\\ S +,+)+++邮政code
+++国家+&放大器,传感器=假);
新LocationTask(getSupportFragmentManager()
getApplicationContext())
.execute(http://maps.googleapis.com/maps/api/geo$c$c/json?address=
+ address.replaceAll(\\ S +,+)
++
+邮政code
+++国家+&放大器,传感器=假);
super.onResume();
}
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_profile);
意向意图= getIntent();
用户名= intent.getStringExtra(SearchResultsActivity.USERNAME);
如果(用户名!= NULL){
的setTitle(用户:+用户名);
}
地址= intent.getStringExtra(SearchResultsActivity.ADDRESS);
邮政code =意图
.getStringExtra(SearchResultsActivity.POSTAL code);
国家= intent.getStringExtra(SearchResultsActivity.COUNTRY);
TextView中的TextView =新的TextView(本);
textView.setTextSize(40);
textView.setText(用户名);
的setContentView(TextView的);
}
}
纵断面布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的FrameLayout
机器人:ID =@ + ID / map_frame
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1.2>
<片段
机器人:ID =@ + ID /图
机器人:名称=com.google.android.gms.maps.MapFragment
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
类=com.google.android.gms.maps.SupportMapFragment/>
< /的FrameLayout>
<的TextView
机器人:ID =@ + ID / TextView的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:文本=TextView的/>
< / LinearLayout中>
位置任务:
公共类LocationTask扩展
AsyncTask的<字符串,字符串,StringBuilder的> {
私人的EditText地址= NULL;
私人邮政的EditText code = NULL;
私人的EditText国家= NULL;
民营背景的applicationContext;
私人FragmentManager fragmentManager;
私人经纬度坐标;
公共LocationTask(FragmentManager fmanager,
上下文的applicationContext){
this.fragmentManager = fmanager;
this.applicationContext =的applicationContext;
}
@覆盖
受保护的StringBuilder doInBackground(字符串... PARAMS){
HTTPGET HTTPGET =新HTTPGET(PARAMS [0]);
HttpClient的客户端=新DefaultHttpClient();
HTT presponse响应;
尝试 {
响应= client.execute(HTTPGET);
StringBuilder的StringBuilder的=新的StringBuilder();
尝试 {
HttpEntity实体= response.getEntity();
InputStream的流= entity.getContent();
INT B:
而((二= stream.read())!= - 1){
stringBuilder.append((char)的B);
}
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
返回StringBuilder的;
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
返回null;
}
@覆盖
保护无效onPostExecute(StringBuilder的结果){
的JSONObject的JSONObject =新的JSONObject();
尝试 {
的JSONObject =新的JSONObject(result.toString());
}赶上(JSONException E){
e.printStackTrace();
}
字符串postal_ code = NULL;
字符串street_address = NULL;
字符串全国= NULL;
尝试 {
字符串状态= jsonObject.getString(身份)的toString()。
如果(status.equalsIgnoreCase(OK)){
JSONArray结果= jsonObject.getJSONArray(结果);
的JSONObject R = results.getJSONObject(0);
JSONArray addressComponentsArray = R
.getJSONArray(address_components);
JSONObject的几何= r.getJSONObject(几何);
JSONObject的locationObj = geometry.getJSONObject(位置);
坐标=新的经纬度(locationObj.getDouble(土地增值税),locationObj.getDouble(LNG));
}
}赶上(JSONException E){
e.printStackTrace();
}
GoogMapHandler GMH =新GoogMapHandler(fragmentManager,的applicationContext);
gmh.initilizeMap(坐标,用户名,AddressInfo的);
}
}
我忘了删除这在我的 ProfileActivity
:
的TextView TextView的=新的TextView(本);
textView.setTextSize(40);
textView.setText(用户名);
的setContentView(TextView的);
删除它,解决我的问题。
I want to display a Google Map on half screen of my profile view. In my onCreate
method I created a new instance of an AsyncTask
to retrieve latitude and longitude from Google servers via JSON. After receiving the result, I call:
GoogMapHandler gmh = new GoogMapHandler(fragmentManager, applicationContext);
gmh.initilizeMap(coord, "Username", "addressInfo");
Here is my GoogMapHandler
class:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.content.Context;
import android.widget.Toast;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import com.google.android.gms.internal.fm;
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;
public class GoogMapHandler {
// Google Map
private GoogleMap googleMap;
private android.support.v4.app.FragmentManager fragmentManager;
private Context applicationContext;
public GoogMapHandler(FragmentManager fmanager, Context applicationContext) {
this.fragmentManager = fmanager;
this.applicationContext = applicationContext;
}
public void initilizeMap(LatLng coord, String username, String addrInfo) {
if (googleMap == null) {
Fragment fragment = this.fragmentManager.findFragmentById(R.id.map);
FragmentTransaction fragTransaction = this.fragmentManager
.beginTransaction();
fragTransaction.detach(fragment);
SupportMapFragment supportmapfragment = (SupportMapFragment) fragment;
googleMap = supportmapfragment.getMap();
if (googleMap == null) {
Toast.makeText(applicationContext,
"Sorry! Unable to create map.", Toast.LENGTH_SHORT)
.show();
} else {
Marker loc = googleMap.addMarker(new MarkerOptions()
.position(coord).title("User: " + username)
.snippet(addrInfo));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coord,
15));
fragTransaction.attach(fragment);
fragTransaction.commit();
}
}
}
}
Now the problem is, that I can't see the map, although I get no errors. As the View is already created, I tried to to "redraw" the fragment to display the map, but it's not working.
How can I achieve this?
Any help would be greatly appreciated.
EDIT:
Here is my ProfileActivity
. As suggested, I used onResume
to create the AsyncTask
.
public class ProfileActivity extends BaseActivity {
private String username, address, postalCode, country;
@Override
protected void onResume() {
Log.i("URL",
"http://maps.googleapis.com/maps/api/geocode/json?address="
+ address.replaceAll("\\s+", "+") + ",+" + postalCode
+ ",+" + country + "&sensor=false");
new LocationTask(getSupportFragmentManager(),
getApplicationContext())
.execute("http://maps.googleapis.com/maps/api/geocode/json?address="
+ address.replaceAll("\\s+", "+")
+ ",+"
+ postalCode
+ ",+" + country + "&sensor=false");
super.onResume();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Intent intent = getIntent();
username = intent.getStringExtra(SearchResultsActivity.USERNAME);
if (username != null) {
setTitle("User: " + username);
}
address = intent.getStringExtra(SearchResultsActivity.ADDRESS);
postalCode = intent
.getStringExtra(SearchResultsActivity.POSTALCODE);
country = intent.getStringExtra(SearchResultsActivity.COUNTRY);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(username);
setContentView(textView);
}
}
Profile Layout:
<?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:id="@+id/map_frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
Location Task:
public class LocationTask extends
AsyncTask<String, String, StringBuilder> {
private EditText address = null;
private EditText postalCode = null;
private EditText country = null;
private Context applicationContext;
private FragmentManager fragmentManager;
private LatLng coord;
public LocationTask(FragmentManager fmanager,
Context applicationContext) {
this.fragmentManager = fmanager;
this.applicationContext = applicationContext;
}
@Override
protected StringBuilder doInBackground(String... params) {
HttpGet httpGet = new HttpGet(params[0]);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
try {
response = client.execute(httpGet);
StringBuilder stringBuilder = new StringBuilder();
try {
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(StringBuilder result) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(result.toString());
} catch (JSONException e) {
e.printStackTrace();
}
String postal_code = null;
String street_address = null;
String country = null;
try {
String status = jsonObject.getString("status").toString();
if (status.equalsIgnoreCase("OK")) {
JSONArray results = jsonObject.getJSONArray("results");
JSONObject r = results.getJSONObject(0);
JSONArray addressComponentsArray = r
.getJSONArray("address_components");
JSONObject geometry = r.getJSONObject("geometry");
JSONObject locationObj = geometry.getJSONObject("location");
coord = new LatLng(locationObj.getDouble("lat"), locationObj.getDouble("lng"));
}
} catch (JSONException e) {
e.printStackTrace();
}
GoogMapHandler gmh = new GoogMapHandler(fragmentManager, applicationContext);
gmh.initilizeMap(coord, "Username", "addressInfo");
}
}
I forgot to remove this in my ProfileActivity
:
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(username);
setContentView(textView);
Removing it, solved my problem.
这篇关于初始化谷歌地图后重绘片段视图 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!